On 12-04-17 08:30 AM, Manfred Lotz wrote:
On Tue, 17 Apr 2012 12:27:32 +0100
Gary Stainburn<gary.stainb...@ringways.co.uk> wrote:
> On Monday 16 April 2012 15:20:35 Paul Johnson wrote:
> > On Mon, Apr 16, 2012 at 06:53:53AM -0700, Paul.G wrote:
> > > Hi All
> > >
> > > Have a question, is it good coding practice to use a& when
> > > calling a subroutine, or it is not required, or it doesn't matter?
> >
> > It's good practice not to use it unless you understand exactly why
> > you would need to use it.
>
> Could someone please expand on this as I seem to always have to do
> this. If I 'use strict' and 'use warnings' I get errors if I don't.
>
One example is this:
#! /usr/bin/perl
use strict;
use warnings;
mysub;
sub mysub {
print "Hi there\n";
}
If you run this you get an error:
Bareword "mysub" not allowed while "strict subs" in use at ./testsub.pl
line 6. Execution of ./testsub.pl aborted due to compilation errors.
Notice the difference in the output. This is why using the & is not
recommended; @_ is propagated forward.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;
# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;
sub foo {
print Dumper \@_;
}
@_ = ( 1 .. 10 );
foo;
&foo;
__END__
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
[updated for today's programmers]
"Show me your code and conceal your interfaces, and I shall continue
to be mystified. Show me your interfaces, and I won't usually need
your code; it'll be obvious."
-- Fred Brooks
Don't be clever; being great is good enough.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/