Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

078274121, info0a ip, should prodicers of software_based services be held liable or not liable for economic injuries, should producers of soft ware based services such as ATMs be held liable for economic injuries suffered when their systems fail?, nisc wwwxxx, wwwxxx0cm, should producers of software-based services, such as atms, be held liable for economic injuries suffered when their systems fail?, wwwxxx0cm, www.webdp.net, Event 9 IIS log failed to write entry

Links

XODOX
Impressum

#1: space in variable referencing

Posted on 2008-01-12 00:03:06 by ciwei2103

Question from a newbie: how to properly use space
when reference to variables, array, hash etc.

please see the code below:
why the first line, second, and third line all output differiently.
thanks.
ciwei

========code below ======
#!/usr/bin/perl

my %ttys =();
open ( WHO, "who|") or die "can;t \n";

while ( <WHO> ){

( $user, $tty ) = split ;
push @{$ttys{ $user}} , $tty;

}

foreach $user ( sort keys %ttys ){

print "first line : $user => @{$ttys {$user}} \n";
print "second line: $user => @ {$ttys { $user}} \n";
print "third line: $user => @{$ttys{$user}} \n";

}

__OUTPUT__
first line : root =>
second line: root => @ { { root}}
third line: root => pts/1 pts/4 pts/6 pts/7 pts/8


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#2: Re: space in variable referencing

Posted on 2008-01-12 17:00:22 by Tom Phoenix

On Jan 11, 2008 3:03 PM, ciwei <ciwei2103@gmail.com> wrote:

> Question from a newbie: how to properly use space
> when reference to variables, array, hash etc.

Don't put any spaces inside a variable name, especially when interpolating it.

> print "first line : $user => @{$ttys {$user}} \n";
> print "second line: $user => @ {$ttys { $user}} \n";
> print "third line: $user => @{$ttys{$user}} \n";

Because of the spaces, the first two use $ttys (a scalar) while the
third uses %ttys (a hash). Although Perl is willing to silently ignore
some whitespace within a variable in ordinary code, quoted whitespace
is significant.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#3: Re: space in variable referencing

Posted on 2008-01-12 22:32:23 by krahnj

ciwei wrote:
> Question from a newbie: how to properly use space
> when reference to variables, array, hash etc.
>
> please see the code below:
> why the first line, second, and third line all output differiently.
> thanks.
> ciwei
>
> ========code below ======
> #!/usr/bin/perl

The next two lines should be:

use warnings;
use strict;

> my %ttys =();
> open ( WHO, "who|") or die "can;t \n";

You should include the $! variable in the error message so you know why
open() failed:

open WHO, 'who |' or die "Cannot open pipe from 'who' $!";

> while ( <WHO> ){
>
> ( $user, $tty ) = split ;
> push @{$ttys{ $user}} , $tty;
>
> }

You should also close the pipe and verify that it closed correctly:

close WHO or warn $! ? "Error closing 'who' pipe: $!"
: "Exit status $? from 'who'";

> foreach $user ( sort keys %ttys ){
>
> print "first line : $user => @{$ttys {$user}} \n";
> print "second line: $user => @ {$ttys { $user}} \n";
> print "third line: $user => @{$ttys{$user}} \n";
>
> }
>
> __OUTPUT__
> first line : root =>
> second line: root => @ { { root}}
> third line: root => pts/1 pts/4 pts/6 pts/7 pts/8

If you had warnings and strict enabled you would have got this output
instead:

$ perl -e'
use warnings;
use strict;
my $user = "root";
my %ttys = ( $user => [ qw(pts/1 pts/4 pts/6 pts/7 pts/8) ] );
print "first line : $user => @{$ttys {$user}} \n";
print "second line: $user => @ {$ttys { $user}} \n";
print "third line: $user => @{$ttys{$user}} \n";
'
Global symbol "$ttys" requires explicit package name at -e line 6.
Global symbol "$ttys" requires explicit package name at -e line 7.
Execution of -e aborted due to compilation errors.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message