CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
about defined

 



makals
Novice

Jan 3, 2011, 6:23 AM

Post #1 of 5 (566 views)
about defined Can't Post

hello

see this everyone:

my $input = shift;

if (defined $input and $input eq "-n") {

$numeric = 1;

$input = shift;



}

what does (defined) mean , and can you explain the first and the socend shift imean we shift from the ARGV and what happend if we shift again to the same variable?




shawnhcorey
Enthusiast


Jan 3, 2011, 7:23 AM

Post #2 of 5 (563 views)
Re: [makals] about defined [In reply to] Can't Post


In Reply To
hello

see this everyone:

my $input = shift;

if (defined $input and $input eq "-n") {

$numeric = 1;

$input = shift;



}

what does (defined) mean , and can you explain the first and the socend shift imean we shift from the ARGV and what happend if we shift again to the same variable?



defined() returns false if the variable is undef. See `perldoc -f defined`.

If you're outside a subroutine, shift() will remove the first item in @ARGV. The second shift will store the next item in the variable. See `perldoc -f shift`.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


makals
Novice

Jan 3, 2011, 9:56 AM

Post #3 of 5 (558 views)
Re: [shawnhcorey] about defined [In reply to] Can't Post

thank you so do the variable store them both i mean the two shifts?


shawnhcorey
Enthusiast


Jan 3, 2011, 10:00 AM

Post #4 of 5 (557 views)
Re: [makals] about defined [In reply to] Can't Post

No, variables that start with a $ store only one value. See `perldoc perldata`.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Karazam
User

Jan 4, 2011, 1:42 AM

Post #5 of 5 (540 views)
Re: [makals] about defined [In reply to] Can't Post

What the code does is this. Imagine you call the script like so:

Code
somescript.pl -n file.txt

The program now shifts the first argument (-n) into $input. It then goes on to check if $input is defined and is "-n". If that is the case it sets $numeric to 1, and shifts the second argument (file.txt) into $input, which will now have the value "file.txt". The difference between this call and the call

Code
somescript.pl file.txt

will be that $numeric is set to 1, which I guess is meant to change some behavior later on in the program. I hope this clarifies things.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives