End-of-Marker
am 28.07.2011 23:36:15 von Emeka
--001636e0ba3c5a7b8504a927f40b
Content-Type: text/plain; charset=ISO-8859-1
Hello All,
Could someone explain what $\ (end-of-marker) is?
I need something detail with simple and complex examples?
Emeka
--
*Satajanus Nig. Ltd
*
--001636e0ba3c5a7b8504a927f40b--
Re: End-of-Marker
am 28.07.2011 23:52:24 von Rob Dixon
On 28/07/2011 22:36, Emeka wrote:
> Hello All,
>
> Could someone explain what $\ (end-of-marker) is?
>
> I need something detail with simple and complex examples?
Take a look at
perldoc perlvar
for documentation on Perl's internal variables like this. This is what
it says
> $\ The output record separator for the print operator. If defined,
> this value is printed after the last of print's arguments.
> Default is "undef". (Mnemonic: you set $\ instead of adding "\n"
> at the end of the print. Also, it's just like $/, but it's what
> you get "back" from Perl.)
So every call to print will output the value of $\, if any, after the
rest of its arguments. By default it is set to undef, so that print will
output only the parameters it is given.
It is occasionally used to automatically append a special end-of-line
sequence for specialized applications, but if you are using an
up-to-date copy of Perl (5.10 or later) and all you want is to print a
newline after each line, then you are better of writing
use feature 'say';
at the start of your program, and thereafter using 'say' instead of
'print'.
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: End-of-Marker
am 29.07.2011 00:04:28 von Rob Dixon
On 28/07/2011 22:58, Emeka wrote:
> Rob,
>
> Thanks... Could you also explain ... $/?
>
>
> Emeka
Please look at the documentation I have indicated in
perldoc perlvar
and come back to this list if you still have questions.
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: End-of-Marker
am 29.07.2011 07:50:20 von timothy adigun
--20cf30025aa05abfb804a92edbe6
Content-Type: text/plain; charset=ISO-8859-1
Hello Emeka,
$/ is the input record separator. This variable holds a newline by
default. When you read records from a file handle, $/ holds the record
delimiter.
e.g Usually, you only read one line at a
time from a file, but if you undefine $/, you can read in a whole,
multiline file at once:
################# your script ###############
#!/usr/bin/perl -w
use strict;
undef $/;
open HANDLE, "file.txt";
my $text =;
print $text;
#################### your file.txt #############
Here 's
text from
a file.
$\ holds the output record separator for the print operator. Usually,
this is a null string, but you can place text in it like this:
$\ - "END_0F_0UTPUT";
print "Hello!";
Hello!END_0F_0UTPUT
Try to also check out perldoc perlvar documentation.
--20cf30025aa05abfb804a92edbe6--
Re: End-of-Marker
am 29.07.2011 16:53:28 von Emeka
--bcaec51a74a8c0b1c604a936718c
Content-Type: text/plain; charset=ISO-8859-1
Rob,
I checked perldoc , and it is pretty like reading Latin. I have been babied
by Factor and co. Perl is something else(or should I say perldoc) , and as
if that is not enough Google is not even finding it easy to search for stuff
like $\ and $/. I am not infinitely lazy , and naturally I use Google to
pick thing out easily. But when Google is not there to back me up ....I
would only depend on your mercy and stackoverflow.
Emeka
On Thu, Jul 28, 2011 at 11:04 PM, Rob Dixon wrote:
> On 28/07/2011 22:58, Emeka wrote:
>
>> Rob,
>>
>> Thanks... Could you also explain ... $/?
>>
>>
>> Emeka
>>
>
> Please look at the documentation I have indicated in
>
> perldoc perlvar
>
> and come back to this list if you still have questions.
>
> Rob
>
--
*Satajanus Nig. Ltd
*
--bcaec51a74a8c0b1c604a936718c--
Re: End-of-Marker
am 29.07.2011 18:39:44 von Rob Dixon
On 29/07/2011 15:53, Emeka wrote:
> Rob,
>
> I checked perldoc , and it is pretty like reading Latin. I have been
> babied by Factor and co. Perl is something else(or should I say
> perldoc) , and as if that is not enough Google is not even finding it
> easy to search for stuff like $\ and $/. I am not infinitely lazy ,
> and naturally I use Google to pick thing out easily. But when Google
> is not there to back me up ....I would only depend on your mercy and
> stackoverflow.
(Please bottom-post your responses to this list Emeka. It is the
standard here, and threads can become very difficult to read if
different people are posting to opposite ends of the quoted text. Thank
you.)
Yes, $/ is a little more convoluted. Basically it defines how much data
the 'readline' function (same as <>) fetches from the file. By default
it is set to "\n", so that each will return a string up to
and including the next newline in the file.
Aside from changing it to something special for unusually-formatted
files, these values are possible:
- $/ = undef will read the whole of the rest of the file, with stopping
at any terminators.
- $/ = "" (the null string) will read everything up to one or more blank
lines
- $/ = \999 (reference to any integer) will read (a maximum of) that
much data as the next record, ignoring record boundaries.
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
perl reference
am 30.07.2011 00:57:36 von Rajeev Prasad
--0-477110965-1311980256=:5390
Content-Type: text/plain; charset=us-ascii
Hello,
from here: http://www.troubleshooters.com/codecorn/littperl/perlsub.htm
i found:
In Perl, you can pass only one kind of argument to a subroutine: a scalar.
To pass any other kind of argument, you need to convert it to a scalar. You
do that by passing a reference to it. A reference to anything is a
scalar. If you're a C programmer you can think of a reference as a pointer
(sort of).
is that still true? date on website is 2003...
thank you.
Rajeev
--0-477110965-1311980256=:5390--
Re: perl reference
am 30.07.2011 01:31:41 von Jim Gibson
On 7/29/11 Fri Jul 29, 2011 3:57 PM, "Rajeev Prasad"
scribbled:
> Hello,
>
> from here: http://www.troubleshooters.com/codecorn/littperl/perlsub.htm
>
> i found:
>
> In Perl, you can pass only one kind of argument to a subroutine: a scalar.
> To pass any other kind of argument, you need to convert it to a scalar. You
> do that by passing a reference to it. A reference to anything is a
> scalar. If you're a C programmer you can think of a reference as a pointer
> (sort of).
>
> is that still true? date on website is 2003...
Yes, that is mostly true. You can pass an array to a subroutine,
call_my_sub(@array);
but an array is just a list (ordered set) of scalars. The contents of the
array are placed into the local @_ array for you within the subroutine.
If you try to pass two arrays:
call_my_sub( @array1, @array2 );
they end up as a single list in the @_ array.
If you pass more than one scalar:
call_my_sub( $x, $y, $z );
they also end up as elements of the @_ array.
So you could also make the statement: "You can only pass an array (list) to
a Perl subroutine", but it is better to understand how Perl passes arguments
to subroutines and avoid making sweeping statements like that.
The situation is made more complicated by the use of prototypes, but I don't
use them, don't understand everything about them, so won't go into that
subject.
If you really want to pass two arrays to a subroutine, you have to use
references:
call_my_sub( \@array1, \@array2 );
sub call_my_sub
{
my( $aref1, $aref2 ) = @_;
my @local1 = @$aref1;
my @local2 = @$aref2;
}
A similar mechanism is used to return values, either scalars or arrays, back
to the calling routine.
See 'perldoc perlsub' for more information about Perl subroutines.
See 'perldoc perlref' and 'perldoc perlreftut' for information
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: perl reference
am 30.07.2011 03:17:29 von timothy adigun
--00248c0d77ac6ca4d104a93f29b5
Content-Type: text/plain; charset=ISO-8859-1
Hi Rajeev,
with the link you provided, the statement "In Perl, you can pass only one
kind of argument to a subroutine: a scalar... a pointer (sort of)." was made
Reference sub-topic. So, it will not be a total truth that one can pass
"only" one kind of argument to subroutine.
Generally in perl the subroutrine default argument is an array "@_", so that
makes it possible to even pass arrays into subroutine! ** Check Example 1,
in the code below.
One can even pass hash to a subroutine with a little trick, 'cos the default
argument of a subroutine is an array "@_". ** Check Example 2, in the code
below.
Finally, I believe that "one" the main purpose of reference in perl is to
help maintain the integrity of data passed to a subroutine.
In Code Example 3 below, two arrays were passed to a sub., inside the sub.
the two array merges to one, and lost identity, then printer with just one
for loop.
But one can keep these array intact, using reference as demonstrated in **
Code Example 4!
To write Object oriented perl one might have to know reference well! Really
it like pointer or passing reference using pointer in c++.
#!/usr/bin/perl -w
use strict;
##Example 1#################
sub getter(@); #declaration of subroutine
my $list="";
my @arr=qw( 12 timo kunle 067 23.90 come_hm);
sub getter(@){ # defination of subroutine
foreach(@_){
$list.="$_\n";
}return $list;
}
print getter(@arr); # print return value
############################################################ ######
##Example 2#################
my %hash=( fistname=>'larry', surname=>'wall',
street=>'earth', value =>'perl');
my $line="";
sub getter_hash{
my %hash=@_; # the trick convert ur @_ to %hash
while(my ($key,$val)=each %hash){
$line.="$key => $val\n";
}
return $line;
}
print getter_hash(%hash);
############################################################ #######
##Example 3#################
sub getter3(@); #declaration of subroutine
$list="";
my @arr1=qw( 12 timo kunle 067 23.90 come_hm);
my @arr2=qw( US 23:46:13 local float GOP_DEBT -q34A);
sub getter3(@){ # defination of subroutine
foreach(@_){
$list.="$_\n";
}return $list;
}
print getter(@arr1,@arr2); # print return value
############################################################ #####
##Example 4#################
sub getter4($$); #declaration of subroutine
my $count=0;
my @arr3=qw( 12 timo kunle 067 23.90 come_hm);
my @arr4=qw( US 23:46:13 local float GOP_DEBT -q34A);
sub getter4($$){ # defination of subroutine
my ($val1,$val2)=@_;
foreach(@{$val1}){++$count;
print "$count:$_\n";
} $count=0;
foreach(@{$val2}){++$count;
print "$count:$_\n";
}
}
getter4(\@arr3,\@arr4); # print return value
Regards.
On Fri, Jul 29, 2011 at 11:57 PM, Rajeev Prasad wrote:
> Hello,
>
> from here: http://www.troubleshooters.com/codecorn/littperl/perlsub.htm
>
> i found:
>
> In Perl, you can pass only one kind of argument to a subroutine: a scalar.
> To pass any other kind of argument, you need to convert it to a scalar. You
> do that by passing a reference to it. A reference to anything is a
> scalar. If you're a C programmer you can think of a reference as a pointer
> (sort of).
>
> is that still true? date on website is 2003...
>
> thank you.
>
> Rajeev
>
--00248c0d77ac6ca4d104a93f29b5--
Re: perl reference
am 30.07.2011 05:23:29 von Shawn Wilson
--0015176f1c600a2c8504a940ec0d
Content-Type: text/plain; charset=ISO-8859-1
As you can see, the answer is a bit more complex than it would seem. If you
go by what ref() says, there 4+. If you go by their fundamental structure
there are just scalar they are interpolated into whatever it should be.
That said, when you deal with most things, using references are the best. It
is much easier to say, 'pass me a hashref and you can define these things'
and you just do:
$ref->{ 'one' } = this;
$ref->{ 'two' } = Some::Module->new( toy );
$returnme = func( $ref );
And all func() has to do is:
my $thing = $_[0];
And then, use $thing->{ 'one' } and $thing->{ 'two' } just like you would
$ref outside your function. And modifying the reference shows up everywhere.
That's it.
On Jul 29, 2011 6:58 PM, "Rajeev Prasad" wrote:
> Hello,
>
> from here: http://www.troubleshooters.com/codecorn/littperl/perlsub.htm
>
> i found:
>
> In Perl, you can pass only one kind of argument to a subroutine: a scalar.
> To pass any other kind of argument, you need to convert it to a scalar.
You
> do that by passing a reference to it. A reference to anything is a
> scalar. If you're a C programmer you can think of a reference as a pointer
> (sort of).
>
> is that still true? date on website is 2003...
>
> thank you.
>
> Rajeev
--0015176f1c600a2c8504a940ec0d--
Re: perl reference
am 30.07.2011 09:34:35 von Emeka
--bcaec5314a9509d78c04a9446eec
Content-Type: text/plain; charset=ISO-8859-1
Timo,
One can even pass hash to a subroutine with a little trick, 'cos the default
argument of a subroutine is an array "@_". ** Check Example 2, in the code
below.
I think this trick is formalized by context rule.
@voo = ("boon", 12, "man", 88);
%coo = @voo;
my $line = "";
while(my ($key, $val) = each %coo){
$line.="$key => $val\n";
}
print $line;
On Sat, Jul 30, 2011 at 2:17 AM, timothy adigun <2teezperl@gmail.com> wrote:
> Hi Rajeev,
> with the link you provided, the statement "In Perl, you can pass only one
> kind of argument to a subroutine: a scalar... a pointer (sort of)." was
> made
> Reference sub-topic. So, it will not be a total truth that one can pass
> "only" one kind of argument to subroutine.
> Generally in perl the subroutrine default argument is an array "@_", so
> that
> makes it possible to even pass arrays into subroutine! ** Check Example 1,
> in the code below.
> One can even pass hash to a subroutine with a little trick, 'cos the
> default
> argument of a subroutine is an array "@_". ** Check Example 2, in the code
> below.
> Finally, I believe that "one" the main purpose of reference in perl is to
> help maintain the integrity of data passed to a subroutine.
> In Code Example 3 below, two arrays were passed to a sub., inside the sub.
> the two array merges to one, and lost identity, then printer with just one
> for loop.
> But one can keep these array intact, using reference as demonstrated in **
> Code Example 4!
> To write Object oriented perl one might have to know reference well! Really
> it like pointer or passing reference using pointer in c++.
>
>
>
> #!/usr/bin/perl -w
> use strict;
>
> ##Example 1#################
>
> sub getter(@); #declaration of subroutine
>
> my $list="";
> my @arr=qw( 12 timo kunle 067 23.90 come_hm);
>
> sub getter(@){ # defination of subroutine
> foreach(@_){
> $list.="$_\n";
> }return $list;
> }
>
> print getter(@arr); # print return value
>
> ############################################################ ######
> ##Example 2#################
>
> my %hash=( fistname=>'larry', surname=>'wall',
> street=>'earth', value =>'perl');
>
> my $line="";
> sub getter_hash{
> my %hash=@_; # the trick convert ur @_ to %hash
> while(my ($key,$val)=each %hash){
> $line.="$key => $val\n";
> }
> return $line;
> }
>
> print getter_hash(%hash);
>
> ############################################################ #######
> ##Example 3#################
>
> sub getter3(@); #declaration of subroutine
>
> $list="";
> my @arr1=qw( 12 timo kunle 067 23.90 come_hm);
> my @arr2=qw( US 23:46:13 local float GOP_DEBT -q34A);
>
> sub getter3(@){ # defination of subroutine
> foreach(@_){
> $list.="$_\n";
> }return $list;
> }
>
> print getter(@arr1,@arr2); # print return value
>
> ############################################################ #####
> ##Example 4#################
>
> sub getter4($$); #declaration of subroutine
>
> my $count=0;
> my @arr3=qw( 12 timo kunle 067 23.90 come_hm);
> my @arr4=qw( US 23:46:13 local float GOP_DEBT -q34A);
>
> sub getter4($$){ # defination of subroutine
> my ($val1,$val2)=@_;
> foreach(@{$val1}){++$count;
> print "$count:$_\n";
> } $count=0;
> foreach(@{$val2}){++$count;
> print "$count:$_\n";
> }
> }
> getter4(\@arr3,\@arr4); # print return value
>
> Regards.
>
>
>
>
> On Fri, Jul 29, 2011 at 11:57 PM, Rajeev Prasad
> wrote:
>
> > Hello,
> >
> > from here: http://www.troubleshooters.com/codecorn/littperl/perlsub.htm
> >
> > i found:
> >
> > In Perl, you can pass only one kind of argument to a subroutine: a
> scalar.
> > To pass any other kind of argument, you need to convert it to a scalar.
> You
> > do that by passing a reference to it. A reference to anything is a
> > scalar. If you're a C programmer you can think of a reference as a
> pointer
> > (sort of).
> >
> > is that still true? date on website is 2003...
> >
> > thank you.
> >
> > Rajeev
> >
>
--
*Satajanus Nig. Ltd
*
--bcaec5314a9509d78c04a9446eec--
Re: perl reference
am 30.07.2011 13:01:08 von jwkrahn
Rajeev Prasad wrote:
> Hello,
Hello,
> from here: http://www.troubleshooters.com/codecorn/littperl/perlsub.htm
>
> i found:
>
> In Perl, you can pass only one kind of argument to a subroutine: a scalar.
> To pass any other kind of argument, you need to convert it to a scalar. You
> do that by passing a reference to it. A reference to anything is a
> scalar. If you're a C programmer you can think of a reference as a pointer
> (sort of).
>
> is that still true? date on website is 2003...
On that web page it says:
NOTE: Modern Perl versions (5.003 and newer) enable you to do function
prototyping somewhat similar to C. Doing so lessens the chance for wierd
runtime errors. Because this page was created before Perl prototyping
was common, much of its code is old school. This will change as time
goes on.
Which leads me to believe that it is based on Perl 4 code and should be
ignored.
Please don't let friends use prototypes in perl.
http://perlmonks.org/?node_id=861966
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: perl reference
am 30.07.2011 16:09:43 von timothy adigun
--20cf300256481ae27c04a949f396
Content-Type: text/plain; charset=ISO-8859-1
Emeka,
Yes in a way, but the point am making here is that
one can also pass hash into a subroutine. Context is everything in Perl!
Caution has to be taken however when converting array into hash. Hash
elements must be even in number, whereas odd numbers of elements could be in
array, one is passing to the hash, thereby an generating error!
#!/usr/bin/perl -wl
use strict;
my @voo = ("boon", 12, "man"); # note this 3 elements array
my %coo = @voo;
my $line = "";
while(my ($key, $val) = each %coo){
$line.="$key => $val\n";
}
print $line; # Oops Error
On Sat, Jul 30, 2011 at 8:34 AM, Emeka wrote:
>
> Timo,
>
> One can even pass hash to a subroutine with a little trick, 'cos the
> default
> argument of a subroutine is an array "@_". ** Check Example 2, in the code
> below.
>
> I think this trick is formalized by context rule.
>
> @voo = ("boon", 12, "man", 88);
> %coo = @voo;
> my $line = "";
> while(my ($key, $val) = each %coo){
> $line.="$key => $val\n";
> }
> print $line;
>
> On Sat, Jul 30, 2011 at 2:17 AM, timothy adigun <2teezperl@gmail.com>wrote:
>
>> Hi Rajeev,
>> with the link you provided, the statement "In Perl, you can pass only one
>> kind of argument to a subroutine: a scalar... a pointer (sort of)." was
>> made
>> Reference sub-topic. So, it will not be a total truth that one can pass
>> "only" one kind of argument to subroutine.
>> Generally in perl the subroutrine default argument is an array "@_", so
>> that
>> makes it possible to even pass arrays into subroutine! ** Check Example 1,
>> in the code below.
>> One can even pass hash to a subroutine with a little trick, 'cos the
>> default
>> argument of a subroutine is an array "@_". ** Check Example 2, in the code
>> below.
>> Finally, I believe that "one" the main purpose of reference in perl is to
>> help maintain the integrity of data passed to a subroutine.
>> In Code Example 3 below, two arrays were passed to a sub., inside the sub.
>> the two array merges to one, and lost identity, then printer with just one
>> for loop.
>> But one can keep these array intact, using reference as demonstrated in **
>> Code Example 4!
>> To write Object oriented perl one might have to know reference well!
>> Really
>> it like pointer or passing reference using pointer in c++.
>>
>>
>>
>> #!/usr/bin/perl -w
>> use strict;
>>
>> ##Example 1#################
>>
>> sub getter(@); #declaration of subroutine
>>
>> my $list="";
>> my @arr=qw( 12 timo kunle 067 23.90 come_hm);
>>
>> sub getter(@){ # defination of subroutine
>> foreach(@_){
>> $list.="$_\n";
>> }return $list;
>> }
>>
>> print getter(@arr); # print return value
>>
>> ############################################################ ######
>> ##Example 2#################
>>
>> my %hash=( fistname=>'larry', surname=>'wall',
>> street=>'earth', value =>'perl');
>>
>> my $line="";
>> sub getter_hash{
>> my %hash=@_; # the trick convert ur @_ to %hash
>> while(my ($key,$val)=each %hash){
>> $line.="$key => $val\n";
>> }
>> return $line;
>> }
>>
>> print getter_hash(%hash);
>>
>> ############################################################ #######
>> ##Example 3#################
>>
>> sub getter3(@); #declaration of subroutine
>>
>> $list="";
>> my @arr1=qw( 12 timo kunle 067 23.90 come_hm);
>> my @arr2=qw( US 23:46:13 local float GOP_DEBT -q34A);
>>
>> sub getter3(@){ # defination of subroutine
>> foreach(@_){
>> $list.="$_\n";
>> }return $list;
>> }
>>
>> print getter(@arr1,@arr2); # print return value
>>
>> ############################################################ #####
>> ##Example 4#################
>>
>> sub getter4($$); #declaration of subroutine
>>
>> my $count=0;
>> my @arr3=qw( 12 timo kunle 067 23.90 come_hm);
>> my @arr4=qw( US 23:46:13 local float GOP_DEBT -q34A);
>>
>> sub getter4($$){ # defination of subroutine
>> my ($val1,$val2)=@_;
>> foreach(@{$val1}){++$count;
>> print "$count:$_\n";
>> } $count=0;
>> foreach(@{$val2}){++$count;
>> print "$count:$_\n";
>> }
>> }
>> getter4(\@arr3,\@arr4); # print return value
>>
>> Regards.
>>
>>
>>
>>
>> On Fri, Jul 29, 2011 at 11:57 PM, Rajeev Prasad
>> wrote:
>>
>> > Hello,
>> >
>> > from here: http://www.troubleshooters.com/codecorn/littperl/perlsub.htm
>> >
>> > i found:
>> >
>> > In Perl, you can pass only one kind of argument to a subroutine: a
>> scalar.
>> > To pass any other kind of argument, you need to convert it to a scalar.
>> You
>> > do that by passing a reference to it. A reference to anything is a
>> > scalar. If you're a C programmer you can think of a reference as a
>> pointer
>> > (sort of).
>> >
>> > is that still true? date on website is 2003...
>> >
>> > thank you.
>> >
>> > Rajeev
>> >
>>
>
>
>
> --
> *Satajanus Nig. Ltd
>
>
> *
>
--20cf300256481ae27c04a949f396--
Re: perl reference
am 30.07.2011 18:12:12 von Emeka
--000e0cd1522428cb6e04a94ba9bc
Content-Type: text/plain; charset=ISO-8859-1
Timo,
Noted!...However, one could work that into his code.
#!/usr/bin/perl -wl
use strict;
my @voo = ("boon", 12, "man"); # note this 3 elements array
die "Must be even elements" if @voo % 2;
my %coo = @voo;
while(my ($key, $val) = each %coo){
$line.="$key => $val\n";
}
print $line;
Emeka
On Sat, Jul 30, 2011 at 3:09 PM, timothy adigun <2teezperl@gmail.com> wrote:
> Emeka,
> Yes in a way, but the point am making here is that
> one can also pass hash into a subroutine. Context is everything in Perl!
> Caution has to be taken however when converting array into hash. Hash
> elements must be even in number, whereas odd numbers of elements could be in
> array, one is passing to the hash, thereby an generating error!
>
>
> #!/usr/bin/perl -wl
> use strict;
>
> my @voo = ("boon", 12, "man"); # note this 3 elements array
> my %coo = @voo;
>
> my $line = "";
> while(my ($key, $val) = each %coo){
> $line.="$key => $val\n";
> }
> print $line; # Oops Error
>
>
>
>
>
> On Sat, Jul 30, 2011 at 8:34 AM, Emeka wrote:
>
>>
>> Timo,
>>
>> One can even pass hash to a subroutine with a little trick, 'cos the
>> default
>> argument of a subroutine is an array "@_". ** Check Example 2, in the code
>> below.
>>
>> I think this trick is formalized by context rule.
>>
>> @voo = ("boon", 12, "man", 88);
>> %coo = @voo;
>> my $line = "";
>> while(my ($key, $val) = each %coo){
>> $line.="$key => $val\n";
>> }
>> print $line;
>>
>> On Sat, Jul 30, 2011 at 2:17 AM, timothy adigun <2teezperl@gmail.com>wrote:
>>
>>> Hi Rajeev,
>>> with the link you provided, the statement "In Perl, you can pass only
>>> one
>>> kind of argument to a subroutine: a scalar... a pointer (sort of)." was
>>> made
>>> Reference sub-topic. So, it will not be a total truth that one can pass
>>> "only" one kind of argument to subroutine.
>>> Generally in perl the subroutrine default argument is an array "@_", so
>>> that
>>> makes it possible to even pass arrays into subroutine! ** Check Example
>>> 1,
>>> in the code below.
>>> One can even pass hash to a subroutine with a little trick, 'cos the
>>> default
>>> argument of a subroutine is an array "@_". ** Check Example 2, in the
>>> code
>>> below.
>>> Finally, I believe that "one" the main purpose of reference in perl is to
>>> help maintain the integrity of data passed to a subroutine.
>>> In Code Example 3 below, two arrays were passed to a sub., inside the
>>> sub.
>>> the two array merges to one, and lost identity, then printer with just
>>> one
>>> for loop.
>>> But one can keep these array intact, using reference as demonstrated in
>>> **
>>> Code Example 4!
>>> To write Object oriented perl one might have to know reference well!
>>> Really
>>> it like pointer or passing reference using pointer in c++.
>>>
>>>
>>>
>>> #!/usr/bin/perl -w
>>> use strict;
>>>
>>> ##Example 1#################
>>>
>>> sub getter(@); #declaration of subroutine
>>>
>>> my $list="";
>>> my @arr=qw( 12 timo kunle 067 23.90 come_hm);
>>>
>>> sub getter(@){ # defination of subroutine
>>> foreach(@_){
>>> $list.="$_\n";
>>> }return $list;
>>> }
>>>
>>> print getter(@arr); # print return value
>>>
>>> ############################################################ ######
>>> ##Example 2#################
>>>
>>> my %hash=( fistname=>'larry', surname=>'wall',
>>> street=>'earth', value =>'perl');
>>>
>>> my $line="";
>>> sub getter_hash{
>>> my %hash=@_; # the trick convert ur @_ to %hash
>>> while(my ($key,$val)=each %hash){
>>> $line.="$key => $val\n";
>>> }
>>> return $line;
>>> }
>>>
>>> print getter_hash(%hash);
>>>
>>> ############################################################ #######
>>> ##Example 3#################
>>>
>>> sub getter3(@); #declaration of subroutine
>>>
>>> $list="";
>>> my @arr1=qw( 12 timo kunle 067 23.90 come_hm);
>>> my @arr2=qw( US 23:46:13 local float GOP_DEBT -q34A);
>>>
>>> sub getter3(@){ # defination of subroutine
>>> foreach(@_){
>>> $list.="$_\n";
>>> }return $list;
>>> }
>>>
>>> print getter(@arr1,@arr2); # print return value
>>>
>>> ############################################################ #####
>>> ##Example 4#################
>>>
>>> sub getter4($$); #declaration of subroutine
>>>
>>> my $count=0;
>>> my @arr3=qw( 12 timo kunle 067 23.90 come_hm);
>>> my @arr4=qw( US 23:46:13 local float GOP_DEBT -q34A);
>>>
>>> sub getter4($$){ # defination of subroutine
>>> my ($val1,$val2)=@_;
>>> foreach(@{$val1}){++$count;
>>> print "$count:$_\n";
>>> } $count=0;
>>> foreach(@{$val2}){++$count;
>>> print "$count:$_\n";
>>> }
>>> }
>>> getter4(\@arr3,\@arr4); # print return value
>>>
>>> Regards.
>>>
>>>
>>>
>>>
>>> On Fri, Jul 29, 2011 at 11:57 PM, Rajeev Prasad
>>> wrote:
>>>
>>> > Hello,
>>> >
>>> > from here:
>>> http://www.troubleshooters.com/codecorn/littperl/perlsub.htm
>>> >
>>> > i found:
>>> >
>>> > In Perl, you can pass only one kind of argument to a subroutine: a
>>> scalar.
>>> > To pass any other kind of argument, you need to convert it to a scalar.
>>> You
>>> > do that by passing a reference to it. A reference to anything is a
>>> > scalar. If you're a C programmer you can think of a reference as a
>>> pointer
>>> > (sort of).
>>> >
>>> > is that still true? date on website is 2003...
>>> >
>>> > thank you.
>>> >
>>> > Rajeev
>>> >
>>>
>>
>>
>>
>> --
>> *Satajanus Nig. Ltd
>>
>>
>> *
>>
>
>
--
*Satajanus Nig. Ltd
*
--000e0cd1522428cb6e04a94ba9bc--
Re: perl reference
am 31.07.2011 01:55:24 von Rob Dixon
On 29/07/2011 23:57, Rajeev Prasad wrote:
> Hello,
>
> from here: http://www.troubleshooters.com/codecorn/littperl/perlsub.htm
>
> i found:
>
> In Perl, you can pass only one kind of argument to a subroutine: a scalar.
> To pass any other kind of argument, you need to convert it to a scalar. You
> do that by passing a reference to it. A reference to anything is a
> scalar. If you're a C programmer you can think of a reference as a pointer
> (sort of).
>
> is that still true? date on website is 2003...
I think this thread is getting a little confused. The page you refer to
is very out of date, but in any case a little misguided.
The only thing you can pass to a subroutine is a LIST (by definition, a
list of scalar values). That list may have zero, one, or many elements,
but all of them are scalars. The list passed to a subroutine appears in
the built-in array @_ at run time.
Yes, it is possible to pass the data in an array to a subroutine by
writing mysub(@data), but that is not 'passing an array' as the array is
decomposed to a list of scalars before it is passed and has to be
rebuilt within the subroutine. That is, there is nothing we can do
'array things' with (aside from @_, and manipulating that is bad
practice) such as push, pop, splice etc unless we write
my @params = @_;
It may be helpful here to read and understand
perldoc "What is the difference between a list and an array"
The same thing happens with a hash. Sure, we can write mysub(%data) to
pass a list of scalars that is amenable to reforming back into a hash
within the subroutine, but it is not 'passing a hash'.
So the page is correct in that a subroutine's parameter list can only
contain scalar values, but there may be any number of them. They may be
passed in many different ways, but the subroutine's code will only ever
see a list of scalars. Even if a call looks like
mysub(@p1, %p2, $p3, subp4())
the values are formed into a single list of scalar values, and presented
to the subroutine code in @_, where there is no distinction between the
source of the parameters.
I hope this clarifies things a little.
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: perl reference
am 31.07.2011 21:48:50 von Rob Dixon
On 31/07/2011 00:55, Rob Dixon wrote:
>
> It may be helpful here to read and understand
>
> perldoc "What is the difference between a list and an array"
That should be
perldoc -q "What is the difference between a list and an array"
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/