Operating System - HP-UX
1838407 Members
3337 Online
110125 Solutions
New Discussion

Re: a little piece of perl scripting help

 
Hanry Zhou
Super Advisor

a little piece of perl scripting help

Hi,

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.
none
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: a little piece of perl scripting help

Hi Hanry:

# 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...
Sean Dale
Trusted Contributor

Re: a little piece of perl scripting help

If it's within a script you could do something like this:

if !($email) {$email = "empty@email.com";}

If the variable email is not set, this will set it to a default value.

Sean
Live life everyday
Hanry Zhou
Super Advisor

Re: a little piece of perl scripting help

James,

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.
none
James R. Ferguson
Acclaimed Contributor

Re: a little piece of perl scripting help

HI (again) Hanry:

'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...
Hanry Zhou
Super Advisor

Re: a little piece of perl scripting help

Sean, James, and all others,

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
none
Hein van den Heuvel
Honored Contributor

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
James R. Ferguson
Acclaimed Contributor

Re: a little piece of perl scripting help

Hi (again) Hanry:

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...