Ending Comma [was Re: [Israel.pm] how to read a file to a Hash?]
Yuval Yaari
yuval at windax.com
Sun Jul 11 04:22:39 PDT 2004
I wasn't aware of the fact that it can be used in sub calls, but then
again, I just remembered that I use it in $dbh->do.
Well, I just woke up and decided to read mail -- big mistake.
I always end hash lists with a comma so it'd be easier to add elements
later, same thing for arrays, and something I was unaware of this morning:
also function calls (esp. $dbh->do...).
Please forgive my stupidity :)
--Yuval
Shlomi Fish said:
> On Sunday 11 July 2004 07:39, Yuval Yaari wrote:
>> And also:
>> send(Client,$line,0,) ??
>> Is that a spare comma after the 0?
>> Does it pass perl -c? strict? warnings? diagnostics? :)
>
> perl -Mstrict -Mdiagnostics -Mwarnings -e 'sub foo {my($i,$j)=@_;print
> +($i+$j),"\n"}foo(3,4,);'
>
> This program runs fine and prints 7.
>
> A terminating comma is perfectly acceptable when specifying a list of
> arguments. This is as opposed to other languages such as C. It was done
> to make sure one can write the ending comma after the last argument and
> then to add more arguments (possibly in subsequent lines). For example:
>
> my %hash =
> (
> 'first_name' => "Yosi",
> 'last_name' => "Steinberg",
> 'phone' => '1-800-800-800',
> # Here you can add more later.
> );
>
> Regards,
>
> Shlomi Fish
>> Might be too early for me to read Perl (7:48am).
>>
>> Have a nice day,
>> --Yuval
>>
>> semuel said:
>> > Hello Liat.
>> >
>> > Just few notes on your code:
>> >> my $port = 2345||shift;
>> >
>> > should be: my $port = shift || 2345;
>> > (you first try to shift, and then if it fails you get your default
>> value.
>> >
>> >> my $line = <FILE> ;
>> >> while ($line)
>> >> {
>> >> send(Client,$line,0,)or die "send() failed: $!";
>> >> $line= <FILE>;
>> >> }
>> >
>> > should be:
>> > my $line;
>> > while ($line=<FILE>) {
>> > send(Client,$line,0,)or die "send() failed: $!";
>> > }
>> > (well, it just look nicer, but does the same)
>> >
>> >> nstore \%hash, 'file';
>> >> my $hashref = retrieve('file');
>> >> send(Client,$hashref,0,)or die "send() failed: $!";
>> >
>> > Oi Vei. Should be:
>> > my $serialized = freeze \%hash;
>> > send(Client,$serialized,0,)or die "send() failed: $!";
>> > and on the client side you should do:
>> > %moved_hash = %{ thaw($moved_serialized) };
>> > (both freeze and thaw are function of Storable)
>> >
>> > Semuel.
>> >
>> > -----Original Message-----
>> > From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On
>> Behalf Of liat
>> > Sent: Saturday, July 10, 2004 10:22 PM
>> > To: Perl in Israel
>> > Subject: RE: [Israel.pm] how to read a file to a Hash?
>> >
>> > hellow,
>> > first of all thank you all for your help, i'm working on a project
>> for my studies, alone without a guide
>> > and your help and directions are very useful and needed!!
>> > actually i'm looking for a perl expert for a privat lessons (by pay
>> of course) , so if anyone can and want to, plz let me know
>> >
>> > about the project, those are the modules of the server and client
>> sides
>> >
>> > the server side===>>
>> > use strict;
>> > use Socket;
>> > use Carp;
>> > use Storable qw(nstore retrieve nstore_fd fd_retrieve);
>> >
>> > my $port = 2345||shift ;
>> > ($port) = $port =~ /^(\d+)$/ or die "invalid port";
>> >
>> > #creating the socket
>> > my $proto = getprotobyname('tcp');
>> > socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket:
>> $!";
>> >
>> > #the 'bind' func'
>> > my $sin= sockaddr_in($port, INADDR_ANY) ; #the 'sin' will
>> insert
>> > bind(Server,$sin )|| die "bind: $!"; # into the bind func'
>> >
>> >
>> > #the 'listen' func'
>> > listen(Server,SOMAXCONN) || die "listen: $!";
>> >
>> > print "Server accepting clients!!!\n";
>> > my $paddr;
>> >
>> > #the 'accept' func'
>> > $paddr=accept(Client,Server) || die $!;
>> >
>> >
>> > if (open(FILE,"url"))
>> > {
>> > my $line = <FILE> ;
>> > while ($line)
>> > {
>> > send(Client,$line,0,)or die "send() failed: $!";
>> > $line= <FILE> ;
>> > }
>> > }
>> >
>> > my %hash = (
>> > "netvision" => {
>> > "adsl" => {
>> > "uname" => 'name',
>> > "upass" => 'pass',
>> > } ,
>> > "cable" => {
>> > "uname" => 'name',
>> > "upass" => 'pass',
>> > } ,
>> > },
>> > "zahav" => {
>> > "adsl" => {
>> > "uname" => 'name',
>> > "upass" => 'pass',
>> > } ,
>> > "cable" => {
>> > "uname" => 'name',
>> > "upass" => 'pass',
>> > } ,
>> > "dialup" => {
>> > "uname" => 'name',
>> > "upass" => 'pass',
>> > } ,
>> > },
>> > );
>> >
>> > nstore \%hash, 'file';
>> > my $hashref = retrieve('file');
>> > send(Client,$hashref,0,)or die "send() failed: $!";
>> >
>> >
>> > the client side===>
>> >
>> > use strict;
>> > use Socket;
>> > my ($remote,$port, $iaddr, $paddr, $proto);
>> > my $line;
>> > my @all_lines;
>> > my %hashref;
>> >
>> > $remote = shift || '217.132.29.105';
>> > $port = 2345||shift ; # random port
>> > if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') } die
>> "No port" unless $port;
>> >
>> > $proto = getprotobyname('tcp');
>> > socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
>> >
>> > $iaddr = inet_aton($remote) || die "no host: $remote";
>> > $paddr = sockaddr_in($port, $iaddr); #the paddr will insert
>> > # into the connect func'
>> > connect(SOCK, $paddr) || die "connect: $!";
>> >
>> >
>> >
>> > my $hashref;
>> >
>> > recv(SOCK, $hashref,300,0) or die "recv() failed: $!";
>> >
>> > my $isp;
>> > my %family;
>> > my $roles;
>> > my $person;
>> > my $kind;
>> > my $user;
>> > my $pass;
>> > while ( ($isp, $roles) = each %$hashref ) { ##need to write
>> %$hashref
>> > in
>> > order to get the
>> > ##hash of the
>> reference
>> > the
>> > 'retrieve' returns
>> > print "$isp: ";
>> > while ( ($kind, $person) = each %$roles ) {
>> > print "$kind=> ";
>> > while ( ($user, $pass) = each %$person ) {
>> > print "$user=$pass ";
>> > }
>> > print "\n";
>> > }
>> > }
>> >
>> >
>> > close (SOCK) || die "close: $!";
>> > exit;
>> >
>> >
>> >
>> >
>> > it's not working of course becuase the sending of the "hashref" if
>> not fits to the client side
>> > and as you see the client need to get the hash and printing it (kind
>> of testing)
>> >
>> > regards
>> > liat koski
>> >
>> >
>> >
>> > -----Original Message-----
>> > From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il]On
>> Behalf Of Gabor Szabo
>> > Sent: Saturday, July 10, 2004 7:20 PM
>> > To: Perl in Israel
>> > Subject: RE: [Israel.pm] how to read a file to a Hash?
>> >
>> >
>> > Liat,
>> > plese show relevant code examples so we can point to where to fix
>> things.
>> >
>> > It is a lot easire to help and it is also good because the examples
>> remain in the archive for the help of future generations....
>> >
>> > Gabor
>> >
>> > _______________________________________________
>> > Perl mailing list
>> > Perl at perl.org.il
>> > http://perl.org.il/mailman/listinfo/perl
>> >
>> >
>> > _______________________________________________
>> > Perl mailing list
>> > Perl at perl.org.il
>> > http://perl.org.il/mailman/listinfo/perl
>> >
>> >
>> >
>> > _______________________________________________
>> > Perl mailing list
>> > Perl at perl.org.il
>> > http://perl.org.il/mailman/listinfo/perl
>>
>> _______________________________________________
>> Perl mailing list
>> Perl at perl.org.il
>> http://perl.org.il/mailman/listinfo/perl
>
> --
>
> ---------------------------------------------------------------------
> Shlomi Fish shlomif at iglu.org.il
> Homepage: http://shlomif.il.eu.org/
>
> Knuth is not God! It took him two days to build the Roman Empire.
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
More information about the Perl
mailing list