For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > question about Data::Dumper









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author question about Data::Dumper
Chen Li

2006-10-01, 6:58 pm

Hi all,

I write a small script to see what is the difference
between calling methods and regular subroutines. Then
I use Data::Dumper to see the data structure. What I
expecte is to print out a string(This is the data1 1
and data2 2) after using Dumper. But here are the
results I get from using Dumper;

$VAR1 = 1;


My question: What does it mean? Does it mean
successfully calling either a subroutine or method?

Thanks in advacne,

Li



##codes

use strict;
use warnings;
use Data::Dumper;
use Bar;

my $call=&Bar::print_result(1,2);#call as a regular
routine
print Dumper $call;
print "\n";
my $call2=Bar->print_result(2);#call as a method
print Dumper $call2;
print "\n";
exit;


##package Bar

package Bar;
use strict;
use warnings;

sub print_result{
my $data1=shift;
my $data2=shift;
print "This is the data1 $data1 and
data2 $data2", "\n";
}

1;

##screen output
This is the data1 1 and data2 2
$VAR1 = 1;

This is the data1 Bar and data2 2
$VAR1 = 1;


________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
nobull67@gmail.com

2006-10-01, 6:58 pm


Chen Li wrote:

> I write a small script to see what is the difference
> between calling methods and regular subroutines.


The argument list.

> Then
> I use Data::Dumper to see the data structure. What I
> expecte is to print out a string(This is the data1 1
> and data2 2) after using Dumper.


Why do you expect that? Your subrouine does not _return_ that string,
it prints it. Since there is no explicit return() in your subroutine
the return value is the last value of the last expression in the
subroutine. In your case the return value of print() - usually 1 unless
there is an error in the output stream.

> But here are the
> results I get from using Dumper;
>
> $VAR1 = 1;
>
> My question: What does it mean?


That means that the value was 1 or '1'.

> Does it mean successfully calling either a subroutine or method?


Er? What does "sucessfully" mean? What it means for a subroutine to
return 1 depends on what the author of the subroutine chose for it to
mean.

Charles K. Clarkson

2006-10-01, 6:58 pm

chen li wrote:

Let's look at the calls:

: my $call=&Bar::print_result(1,2);
: my $call2=Bar->print_result(2);

$call gets whatever is returned by Bar::print_result().
As we see below, Bar::print_result() always returns 1.


Now. we'll look at what the sub returns:

: sub print_result{
: my $data1=shift;
: my $data2=shift;
: print "This is the data1 $data1 and data2 $data2", "\n";
: }

Unless the return() statement is used, a sub routine
returns the value of the last operation or assignment. This
subroutine returns the success of the print. If the print is
successful, this subroutine, whether called as a method or as
a subroutine, will always return 1.

Since $call will always be set to 1, we have confirmation
that the call to both the subroutine and the method were
successful.


: My question: What does it mean? Does it mean
: successfully calling either a subroutine or method?


: This is the data1 1 and data2 2

@_ = (1, 2)

: $VAR1 = 1;

Sub routine call was successful.

: This is the data1 Bar and data2 2

@_ = ('Bar', 2)

: $VAR1 = 1;

Sub routine call was successful.


This may be a more useful test.

use strict;
use warnings;
use Bar;

print "Sub routine call:\n\tBar::print_result(1,2);\n";
my $call = Bar::print_result(1,2);


print "\n\nMethod call:\n\tBar->print_result(1,2);\n";
$call = Bar->print_result(1,2);

__END__

package Bar;

use strict;
use warnings;
use Data::Dumper 'Dumper';

sub print_result{
print Dumper \@_;
}

1;



HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer

254 968-8328

Don't tread on my bandwidth. Trim your posts.

Chen Li

2006-10-01, 6:58 pm


> This may be a more useful test.
>
> use strict;
> use warnings;
> use Bar;
>
> print "Sub routine
> call:\n\tBar::print_result(1,2);\n";
> my $call = Bar::print_result(1,2);
>
>
> print "\n\nMethod
> call:\n\tBar->print_result(1,2);\n";
> $call = Bar->print_result(1,2);
>
> __END__
>
> package Bar;
>
> use strict;
> use warnings;
> use Data::Dumper 'Dumper';
>
> sub print_result{
> print Dumper \@_;
> }
>
> 1;


Hi Charles,

Thank you very much for the script and the
explanations.

Li

________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com