- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: a little piece of perl scripting help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2007 11:12 AM
02-12-2007 11:12 AM
a little piece of perl scripting help
I want to find first if the variable $email is equal empty or not, if it is the empty, I want to assign a default email address "empty@email.com". the empty strings could be any length in the field.
How do I do that? Appreciate your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2007 11:21 AM
02-12-2007 11:21 AM
Re: a little piece of perl scripting help
# perl -le '$email=shift;$email="xyz\@abc.com" unless defined $email;print $email'
...with arrays or lists you can drop the 'defined' and say:
# perl -le 'die "Pass me something!\n" unless @ARGV'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2007 11:31 AM
02-12-2007 11:31 AM
Re: a little piece of perl scripting help
if !($email) {$email = "empty@email.com";}
If the variable email is not set, this will set it to a default value.
Sean
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2007 11:31 AM
02-12-2007 11:31 AM
Re: a little piece of perl scripting help
Thanks for the prompt response.
I actually want this piece of the code to be placed in a existing perl script, so what I really should do is to add following line in the appropriate place:
$email=shift;$email="xyz\@abc.com" unless defined $email
Is this correct?
Also, what is $email=shift mean?
This is a cgi script, the variable $email defined as 70 character long field, so it could be not filled at all which means empty.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2007 11:41 AM
02-12-2007 11:41 AM
Re: a little piece of perl scripting help
'shift' takes the first value from the front (left most side) of an array, returns it and trims the array by the one element removed.
Thus:
@a = qw( a b c );
$x = shift @a;
print $s
...prints "a"
...and leaves @a containing only the elements 'b' and 'c'.
In the absence of an array argument, 'shift' uses @_ --- the arguments passed to a subroutine or @ARGV --- the argument list passed to the program.
Hence, whether or not you can use the original example I provided, or not will depend on your code. The concept of testing using 'defined' answers you question, however.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2007 01:46 PM
02-12-2007 01:46 PM
Re: a little piece of perl scripting help
I have tried to add either one of following statement in my attached script, however, both of them are not working. When I test, the entire perl script seems not working.
I respectively tried add the line after the stetement "$email = $_[0];" Please see attachment.
if !($email) {$email = "empty@email.com";};
$email=shift;$email="xyz\@abc.com" unless defined $email
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2007 03:45 PM
02-12-2007 03:45 PM
Re: a little piece of perl scripting help
Hanry,
My first though was also that you need to use a simple:
$email = $_[0];
$email = "empty@email.com" unless $email;
In the above $email is tested for 'empty', '0', or undefined.
But reading your clarification it sound like you need to expect a space-filled
variable. It the is the case, then the simple test you need is for 'non-whitespace'.
Try this:
$email = $_[0];
$email = "empty@email.com" unless ($email =~ /\S/);
Which is similar in effect to the positive test for 'all filled with whitespace or empty' :
$email = $_[0];
$email = "empty@email.com" if ($email =~ /^\s*$/);
Two more test lines below.
Regards,
Hein.
$ perl -le '$email="x"; $x= ($email =~/\S/) ? "something" : "blank"; print $x'
something
$ perl -le '$email=" "; $x= ($email =~/^\s*$/) ? "blank" : "something"; print $x
'
blank
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2007 08:44 AM
02-13-2007 08:44 AM
Re: a little piece of perl scripting help
Given the subroutine you presented, you could do use it along these lines:
sub check_email {
...
}
$who = shift;
$address = check_email($who) ? $who : "empty\@email.com";
print "$address\n";
If '$who' is empty or consists only of whitespace you will use the default email address of "empty@email.com".
If '$who' is defined (isn't empty or isn't just whitespace), it will be checked for validity. If valid, '$address' will contain '$who' otherwise '$address' will be set to "empty\@email.com".
I used '$who = shift' as if the whole script consisted of nothing but your subroutine followed by the three lines of code I show. In that fashion, if the script were called 'emailme' I could do:
# ./emailme hanry@somewhere.net
# ./emailme
(or)
# ./emailme this_is_not_valid
Regards!
...JRF...