Re: Perl Critic and subroutine signatures

2017-03-29 Thread SSC_perl
> On Mar 29, 2017, at 3:47 PM, Kevin Phair  wrote:
> 
> Something like
> 
> [-Subroutines::ProhibitSubroutinePrototypes]

Thanks.  I ran across that myself on perlmaven.com right before I went 
to lunch.  I was hoping to find a page on CPAN or even the Perl Critic web site 
that would list all the exceptions, but unless I just don’t know where to look, 
it’s simply not available.  Now watch - someone will find that list and prove 
me wrong. ;)

——

O.K., if you install P::C on your own machine, you can run:

perlcritic -list

to get a list of them.

Also, some good news.  It looks like P::C is now under new management, 
so we’ll probably be seeing an update before too much longer.

https://github.com/Perl-Critic/Perl-Critic/issues/713

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




Re: Perl Critic and subroutine signatures

2017-03-29 Thread Kevin Phair
Something like

[-Subroutines::ProhibitSubroutinePrototypes]

in your .perlcriticrc should do the trick.

You can also do it inline with something like

## no critic (Subroutines::ProhibitSubroutinePrototypes)
sub subname ( ... ) {
## use critic

On Wed, Mar 29, 2017 at 1:16 PM, SSC_perl  wrote:

> Does anyone know how to keep Perl Critic from complaining about subroutine
> signatures?  I’m getting a massive amount of these types of warnings:
>
> Subroutine prototypes used at line...
>
> It also thinks that postfix dereferencing is a magic variable.  Is this
> because PC hasn’t been updated for a couple years, or is there an add-on
> that takes care of these situations?
>
> Thanks,
> Frank
>


Re: Perl Critic and subroutine signatures

2017-03-29 Thread Uri Guttman

On 03/29/2017 01:32 PM, SSC_perl wrote:
On Mar 29, 2017, at 10:19 AM, Uri Guttman <mailto:u...@stemsystems.com>> wrote:


i would ask why are you using prototypes?


I’m not using prototypes.  I’m using subroutine signatures.  Perl 
Critic only thinks they are prototypes.
ok, i just got that. perl critic has ways to shut down any of its 
warnings. look for prototype warnings and you can disable them. as you 
said, it needs to be updated to handle signatures.


uri



Re: Perl Critic and subroutine signatures

2017-03-29 Thread SSC_perl
> On Mar 29, 2017, at 10:19 AM, Uri Guttman  wrote:
> 
> i would ask why are you using prototypes?

I’m not using prototypes.  I’m using subroutine signatures.  Perl 
Critic only thinks they are prototypes.

Re: Perl Critic and subroutine signatures

2017-03-29 Thread Uri Guttman

On 03/29/2017 01:16 PM, SSC_perl wrote:
Does anyone know how to keep Perl Critic from complaining about 
subroutine signatures?  I’m getting a massive amount of these types of 
warnings:


Subroutine prototypes used at line...



i would ask why are you using prototypes? they are rarely useful 
(especially as arg checkers). the primary good use is changing how a sub 
is parsed when called (unary, taking a block arg, etc).


uri



Perl Critic and subroutine signatures

2017-03-29 Thread SSC_perl
Does anyone know how to keep Perl Critic from complaining about 
subroutine signatures?  I’m getting a massive amount of these types of warnings:

Subroutine prototypes used at line...

It also thinks that postfix dereferencing is a magic variable.  Is this 
because PC hasn’t been updated for a couple years, or is there an add-on that 
takes care of these situations?

Thanks,
Frank

Re: script that passes args to subroutine works ok, after I unpack the args. But perlcritic says I'm not unpacking?

2017-02-25 Thread khalil zakaria Zemmoura
Naming multiple variables with the same name like you did ($args, %args) is
a
bad idea.  because when you want to access the value of the hash %args
($args{FN}) you are accessing in reality what was shifted in the scalar
$args and not the hash %args
because perl use simbolic reference.
here is a link that explain that

https://perlmaven.com/symbolic-reference-in-perl
*Khalil Zakaria Zemmoura*
*Visiteur Médical EST*
*Laboratoire NOVOMEDIS*


Re: script that passes args to subroutine works ok, after I unpack the args. But perlcritic says I'm not unpacking?

2017-02-25 Thread ZEMMOURA Khalil Zakaria


On Sun, Jan 15, 2017 at 12:45:41PM -0800, al...@myfastmail.com wrote:
> Hi
> 
> On Sun, Jan 15, 2017, at 12:23 PM, Илья Рассадин wrote:
> > I think, you can use this aproach
> 
> If I use either of those
> 
> 
>   sub modrec {
> - my %args = %{ shift @_ };
> + my ($args) = @_;
> 
> 30my $fn = $args{FN};
>   my $ar = $args{AR};
>   my $ad = $args{AD};
>   my @dr = @{$args{DR}};
> 
>   return;
>   }
> 
> or
> 
>   sub modrec {
> - my %args = %{ shift @_ };
> + my $args = shift @_;
> 
> 30my $fn = $args{FN};
>   my $ar = $args{AR};
>   my $ad = $args{AD};
>   my @dr = @{$args{DR}};
> 
>   return;
>   }

Naming multiple variables with the same name like you did ($args, %args) is a
bad idea.  because when you want to access the value of the hash %args
($args{FN}) you are accessing in reality what was shifted in the scalar $args 
and not the hash %args
because perl use simbolic reference.
here is a link that explain that

https://perlmaven.com/symbolic-reference-in-perl

> 
> the script won't even execute.  Both give the same error
> 
>   perl /home/aj/test.pl
>   Global symbol "%args" requires explicit package name at 
> /home/aj/test.pl line 30.
>   Global symbol "%args" requires explicit package name at 
> /home/aj/test.pl line 31.
>   Global symbol "%args" requires explicit package name at 
> /home/aj/test.pl line 32.
>   Global symbol "%args" requires explicit package name at 
> /home/aj/test.pl line 33.
>   Execution of /home/aj/test.pl aborted due to compilation errors.
> 
> Which is weird since after I make those changes I'm not even using "%args" 
> anymore.
> 
> AJ
> 
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 

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




Re: script that passes args to subroutine works ok, after I unpack the args. But perlcritic says I'm not unpacking?

2017-01-15 Thread Chas. Owens
On Sun, Jan 15, 2017, 16:19  wrote:

Hi,

On Sun, Jan 15, 2017, at 01:01 PM, Shawn H Corey wrote:
> > Is there a different, recommended way?
>
> Nothing's wrong. perlcritic does not this valid method, that's all.
>
> TIMTOWTDI (There Is More Than One Way To Do It.)

Hm, ok.  As long as it's not wrong/broken in some weird way.

I kept getting scolded to "check your code with perlcritic" as if that's
the *right* way to do it.  So when I kept getting that perlcritic-ism, I
started looking around for why.  LOTS of post telling you different things
NOT to do, but nothing that explained in a way I could understand why what
I was doing wasn't cool.

I must've read & parsed that "--verbose 11" output  a dozen times.  Just
isn't understandble :-(

So, anyway, sticking with what I got until if/when I understand the point!

Thanks alot.

AJ


In this case, what you are doing "wrong" is trusting the caller of the
function.  Good defensive practices will save you a ton of headache in the
future.  Your code expects the first (and only) argument to be a hashref.
Proper checking of the arguments will save maintainers tons of time.  For
instance, examine these error messages:

Can't use string ("FN") as a HASH ref while "strict refs" in use at sig.pl
line 9.
Not a HASH reference at sig.pl line 9.

And now look at these error messages:

wrong number of arguments at sig.pl line 13.
main::better("FN", 1, "AR", 1, "AD", 1, "DR", 1) called at sig.pl
line 34
eval {...} called at sig.pl line 36
argument isn't a hashref at sig.pl line 15.
main::better(ARRAY(0x7fc08b0292a8)) called at sig.pl line 41
eval {...} called at sig.pl line 43

These error messages were produced by the following code.  In both cases
(calling with a list instead of a hashref and calling with an arrayref
instead of a hashref), the first set of error messages only tell you where
the code blew up, not where the programmer made an error.  In the second
set of error messages, you get better location information and better
overall information.

Now, this is obviously better for the maintainer, but it comes at the cost
of more verbose code.  If this is a one-off script, then it probably isn't
worthwhile; however, if this is code that is meant to live for any
appreciable length of time, then verbode handling is best.

#!/usr/bin/perl

use strict;
use Carp;
use warnings;


sub not_so_good {
my %args = %{ shift @_ };
}

sub better {
croak "wrong number of arguments" unless @_ == 1;
my $arg = shift @_;
croak "argument isn't a hashref" unless ref $arg eq ref {};
my %args = %$arg;
}

eval {
not_so_good(FN => 1, AR => 1, AD => 1, DR => 1);
1;
} or do {
print "got error: $@";
};

eval {
not_so_good([FN => 1, AR => 1, AD => 1, DR => 1]);
1;
} or do {
print "got error: $@";
};

eval {
better(FN => 1, AR => 1, AD => 1, DR => 1);
1;
} or do {
print "got error: $@";
};

eval {
better([FN => 1, AR => 1, AD => 1, DR => 1]);
1;
} or do {
print "got error: $@";
};


Re: script that passes args to subroutine works ok, after I unpack the args. But perlcritic says I'm not unpacking?

2017-01-15 Thread Rob Dixon
Hi Alan

You are unpacking `@_` in a way, but perlcritic doesn't recognise doing it this 
way.

I think you'd be better off without dereferencing the hash, and using a slice 
to assign your local variables. I would write your subroutine like this

sub modrec {
my ($args) = @_;

my ($fn, $ar, $ad, $dr) = @{$args}{qw/ FN AR AD DR /};
...
}

And if you must, you can dereference the array with

my @dr = @$dr;

but it's generally better to access the elements directly from the reference, 
with `$dr->[0]`, `$dr->[1]` etc.

Rob


   

On 15 January 2017 20:09:53 GMT+00:00, al...@myfastmail.com wrote:
>Hi,
>
>I have a simple script with a subroutine that I pass scalar & array
>arguments to,
>
>   #!/usr/bin/perl
>
>   use 5.01201;
>   use strict;
>   use warnings;
>
>   my $this_fn = "input.txt";
>   my @this_dr = qw(
> /path/1
> /path/2
>   );
>   my $this_rn = "recname";
>   my $this_ad = "1.2.3.4.";
>
>   sub modrec {
>   my %args = %{ shift @_ };
>
>   my $fn = $args{FN};
>   my $ar = $args{AR};
>   my $ad = $args{AD};
>   my @dr = @{$args{DR}};
>
>   return;
>   }
>
>   modrec (
>{
>  FN=>$this_fn,
>  DR=>\@this_dr,
>  AR=>$this_rn,
>  AD=>$this_ad,
>}
>   );
>
>The script *executes* just fine.
>
>But when I exec perlcritic on it
>
>   perlcritic --verbose 11 -harsh test.pl
>   Always unpack @_ first at line 15, near 'sub modrec {'.
> Subroutines::RequireArgUnpacking (Severity: 4)
>   Subroutines that use `@_' directly instead of unpacking the
>arguments to
>   local variables first have two major problems. First, they 
> are
>very hard
>   to read. If you're going to refer to your variables by 
> number
>instead of
>   by name, you may as well be writing assembler code! Second, 
> `@_'
>   contains aliases to the original variables! If you modify 
> the
>contents
>   of a `@_' entry, then you are modifying the variable 
> outside of
>your
>   subroutine. For example:
>
>  sub print_local_var_plus_one {
>  my ($var) = @_;
>  print ++$var;
>  }
>  sub print_var_plus_one {
>  print ++$_[0];
>  }
>
>  my $x = 2;
>  print_local_var_plus_one($x); # prints "3", $x is still 2
>  print_var_plus_one($x);   # prints "3", $x is now 3 !
>  print $x; # prints "3"
>
>   This is spooky action-at-a-distance and is very hard to 
> debug if
>it's
>   not intentional and well-documented (like `chop' or 
> `chomp').
>
>   An exception is made for the usual delegation idiom
>   `$object->SUPER::something( @_ )'. Only `SUPER::' and 
> `NEXT::'
>are
>   recognized (though this is configurable) and the argument 
> list
>for the
>   delegate must consist only of `( @_ )'.
>
>What's wrong with the way I'm unpacking the arguments passed to the
>subroutine,
>
>   my %args = %{ shift @_ };
>
>Is there a different, recommended way?
>
>AJ
>
>-- 
>To unsubscribe, e-mail: beginners-unsubscr...@perl.org
>For additional commands, e-mail: beginners-h...@perl.org
>http://learn.perl.org/

-- 
Sent from Kaiten Mail. Please excuse my brevity.

Re: script that passes args to subroutine works ok, after I unpack the args. But perlcritic says I'm not unpacking?

2017-01-15 Thread alanj
Hi,

On Sun, Jan 15, 2017, at 01:01 PM, Shawn H Corey wrote:
> > Is there a different, recommended way?
> 
> Nothing's wrong. perlcritic does not this valid method, that's all.
> 
> TIMTOWTDI (There Is More Than One Way To Do It.)

Hm, ok.  As long as it's not wrong/broken in some weird way.

I kept getting scolded to "check your code with perlcritic" as if that's the 
*right* way to do it.  So when I kept getting that perlcritic-ism, I started 
looking around for why.  LOTS of post telling you different things NOT to do, 
but nothing that explained in a way I could understand why what I was doing 
wasn't cool.

I must've read & parsed that "--verbose 11" output  a dozen times.  Just isn't 
understandble :-(

So, anyway, sticking with what I got until if/when I understand the point!

Thanks alot.

AJ

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




Re: script that passes args to subroutine works ok, after I unpack the args. But perlcritic says I'm not unpacking?

2017-01-15 Thread Shawn H Corey
On Sun, 15 Jan 2017 12:09:53 -0800
al...@myfastmail.com wrote:

> What's wrong with the way I'm unpacking the arguments passed to the
> subroutine,
> 
>   my %args = %{ shift @_ };
> 
> Is there a different, recommended way?

Nothing's wrong. perlcritic does not this valid method, that's all.

TIMTOWTDI (There Is More Than One Way To Do It.)


-- 
Don't stop where the ink does.

Shawn H Corey

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




Re: script that passes args to subroutine works ok, after I unpack the args. But perlcritic says I'm not unpacking?

2017-01-15 Thread Илья Рассадин

Hi!

You forgot arrow operator

$args->{'FN'}, not $args{'FN'}


15.01.17 23:45, al...@myfastmail.com пишет:

Hi

On Sun, Jan 15, 2017, at 12:23 PM, Илья Рассадин wrote:

I think, you can use this aproach

If I use either of those


sub modrec {
-   my %args = %{ shift @_ };
+   my ($args) = @_;

30  my $fn = $args{FN};
my $ar = $args{AR};
my $ad = $args{AD};
my @dr = @{$args{DR}};

return;
}

or

sub modrec {
-   my %args = %{ shift @_ };
+   my $args = shift @_;

30  my $fn = $args{FN};
my $ar = $args{AR};
my $ad = $args{AD};
my @dr = @{$args{DR}};

return;
}

the script won't even execute.  Both give the same error

perl /home/aj/test.pl
Global symbol "%args" requires explicit package name at 
/home/aj/test.pl line 30.
Global symbol "%args" requires explicit package name at 
/home/aj/test.pl line 31.
Global symbol "%args" requires explicit package name at 
/home/aj/test.pl line 32.
Global symbol "%args" requires explicit package name at 
/home/aj/test.pl line 33.
Execution of /home/aj/test.pl aborted due to compilation errors.

Which is weird since after I make those changes I'm not even using "%args" 
anymore.

AJ



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




Re: script that passes args to subroutine works ok, after I unpack the args. But perlcritic says I'm not unpacking?

2017-01-15 Thread alanj
Hi

On Sun, Jan 15, 2017, at 12:23 PM, Илья Рассадин wrote:
> I think, you can use this aproach

If I use either of those


sub modrec {
-   my %args = %{ shift @_ };
+   my ($args) = @_;

30  my $fn = $args{FN};
my $ar = $args{AR};
my $ad = $args{AD};
my @dr = @{$args{DR}};

return;
}

or

sub modrec {
-   my %args = %{ shift @_ };
+   my $args = shift @_;

30  my $fn = $args{FN};
my $ar = $args{AR};
my $ad = $args{AD};
my @dr = @{$args{DR}};

return;
}

the script won't even execute.  Both give the same error

perl /home/aj/test.pl
Global symbol "%args" requires explicit package name at 
/home/aj/test.pl line 30.
Global symbol "%args" requires explicit package name at 
/home/aj/test.pl line 31.
Global symbol "%args" requires explicit package name at 
/home/aj/test.pl line 32.
Global symbol "%args" requires explicit package name at 
/home/aj/test.pl line 33.
Execution of /home/aj/test.pl aborted due to compilation errors.

Which is weird since after I make those changes I'm not even using "%args" 
anymore.

AJ

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




Re: script that passes args to subroutine works ok, after I unpack the args. But perlcritic says I'm not unpacking?

2017-01-15 Thread Илья Рассадин

Hi!

I think, you can use this aproach

sub modrec {

my ($args) = @_; # or my $args = shift @_; use what you like more

my $fn = $args->{'FN'};



}



15.01.17 23:09, al...@myfastmail.com пишет:

Hi,

I have a simple script with a subroutine that I pass scalar & array arguments 
to,

#!/usr/bin/perl

use 5.01201;
use strict;
use warnings;

my $this_fn = "input.txt";
my @this_dr = qw(
  /path/1
  /path/2
);
my $this_rn = "recname";
my $this_ad = "1.2.3.4.";

sub modrec {
my %args = %{ shift @_ };

my $fn = $args{FN};
my $ar = $args{AR};
my $ad = $args{AD};
my @dr = @{$args{DR}};

return;
}

modrec (
 {
   FN=>$this_fn,
   DR=>\@this_dr,
   AR=>$this_rn,
   AD=>$this_ad,
 }
);

The script *executes* just fine.

But when I exec perlcritic on it

perlcritic --verbose 11 -harsh test.pl
Always unpack @_ first at line 15, near 'sub modrec {'.
  Subroutines::RequireArgUnpacking (Severity: 4)
Subroutines that use `@_' directly instead of unpacking the 
arguments to
local variables first have two major problems. First, they 
are very hard
to read. If you're going to refer to your variables by 
number instead of
by name, you may as well be writing assembler code! Second, 
`@_'
contains aliases to the original variables! If you modify 
the contents
of a `@_' entry, then you are modifying the variable 
outside of your
subroutine. For example:

   sub print_local_var_plus_one {
   my ($var) = @_;
   print ++$var;
   }
   sub print_var_plus_one {
   print ++$_[0];
   }

   my $x = 2;
   print_local_var_plus_one($x); # prints "3", $x is still 2
   print_var_plus_one($x);   # prints "3", $x is now 3 !
   print $x; # prints "3"

This is spooky action-at-a-distance and is very hard to 
debug if it's
not intentional and well-documented (like `chop' or 
`chomp').

An exception is made for the usual delegation idiom
`$object->SUPER::something( @_ )'. Only `SUPER::' and 
`NEXT::' are
recognized (though this is configurable) and the argument 
list for the
delegate must consist only of `( @_ )'.

What's wrong with the way I'm unpacking the arguments passed to the subroutine,

my %args = %{ shift @_ };

Is there a different, recommended way?

AJ



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




script that passes args to subroutine works ok, after I unpack the args. But perlcritic says I'm not unpacking?

2017-01-15 Thread alanj
Hi,

I have a simple script with a subroutine that I pass scalar & array arguments 
to,

#!/usr/bin/perl

use 5.01201;
use strict;
use warnings;

my $this_fn = "input.txt";
my @this_dr = qw(
  /path/1
  /path/2
);
my $this_rn = "recname";
my $this_ad = "1.2.3.4.";

sub modrec {
my %args = %{ shift @_ };

my $fn = $args{FN};
my $ar = $args{AR};
my $ad = $args{AD};
my @dr = @{$args{DR}};

return;
}

modrec (
 {
   FN=>$this_fn,
   DR=>\@this_dr,
   AR=>$this_rn,
   AD=>$this_ad,
 }
);

The script *executes* just fine.

But when I exec perlcritic on it

perlcritic --verbose 11 -harsh test.pl
Always unpack @_ first at line 15, near 'sub modrec {'.
  Subroutines::RequireArgUnpacking (Severity: 4)
Subroutines that use `@_' directly instead of unpacking the 
arguments to
local variables first have two major problems. First, they 
are very hard
to read. If you're going to refer to your variables by 
number instead of
by name, you may as well be writing assembler code! Second, 
`@_'
contains aliases to the original variables! If you modify 
the contents
of a `@_' entry, then you are modifying the variable 
outside of your
subroutine. For example:

   sub print_local_var_plus_one {
   my ($var) = @_;
   print ++$var;
   }
   sub print_var_plus_one {
   print ++$_[0];
   }

   my $x = 2;
   print_local_var_plus_one($x); # prints "3", $x is still 2
   print_var_plus_one($x);   # prints "3", $x is now 3 !
   print $x; # prints "3"

This is spooky action-at-a-distance and is very hard to 
debug if it's
not intentional and well-documented (like `chop' or 
`chomp').

An exception is made for the usual delegation idiom
`$object->SUPER::something( @_ )'. Only `SUPER::' and 
`NEXT::' are
recognized (though this is configurable) and the argument 
list for the
delegate must consist only of `( @_ )'.

What's wrong with the way I'm unpacking the arguments passed to the subroutine,

my %args = %{ shift @_ };

Is there a different, recommended way?

AJ

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




Re: close dbh in the subroutine

2014-05-08 Thread David Precious
On Thu, 08 May 2014 16:54:32 +0400
Jeff Pang  wrote:

>  Hi,
> 
> I created a $dbh object in the main body, and pass it to a
> subroutine, then close the $dbh in the subroutine, does it  influence
> the $dbh in the main?

Yes.  It's a reference to the same DBI object.



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




close dbh in the subroutine

2014-05-08 Thread Jeff Pang
 Hi,

I created a $dbh object in the main body, and pass it to a subroutine, then 
close the $dbh in the subroutine, does it  influence the $dbh in the main?

$dbh = DBI->connect($data_source, $username, $auth, \%attr);
my_func($dbh);
$dbh->do(something); # will this broke?

sub my_func {
my $dbh=shift;
...
$dbh->close;
}

Thanks.



Re: How do I pass arrays into a subroutine

2013-09-07 Thread Karol Bujaček

Hello,

On 09/07/2013 02:15 PM, Dr.Ruud wrote:

On 07/09/2013 13:43, Karol Bujaček wrote:


   print Dumper(@_);# just look how the @_ looks like


Consider: print Dumper( \@_ );


I chose Data::Dumper::Simple 
<http://search.cpan.org/~ovid/Data-Dumper-Simple-0.11/lib/Data/Dumper/Simple.pm> 
which has such sentence in the documentation: ‘Note that there's no need 
to even take a reference to the variables.’ Therefore I expected that 
filters used in the D::D::S uses references somehow automatically. Is 
this guess wrong?





   my($animals, $digits) = @_;

   my @animals = @$animals; # dereference
   my @digits = @$digits;   # dereference


The comment is inaccurate.

Why would you want to copy the arrays?


Because I don't want to change an array by my subroutine, as Shawn H 
Corey answered already.


Well, this may or may not be eventual's intention, apt remark.


Best regards,
Karol

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




Re: How do I pass arrays into a subroutine

2013-09-07 Thread Shawn H Corey
On Sat, 07 Sep 2013 14:15:39 +0200
"Dr.Ruud"  wrote:

> On 07/09/2013 13:43, Karol Bujaček wrote:
> 
> >print Dumper(@_);# just look how the @_ looks like
> 
> Consider: print Dumper( \@_ );
> 
> 
> >my($animals, $digits) = @_;
> >
> >my @animals = @$animals; # dereference
> >my @digits = @$digits;   # dereference
> 
> The comment is inaccurate.
> 
> Why would you want to copy the arrays?

So you could can their contents without the contents of the arrays in
the caller being changed.

> 
> 
> >print Dumper(@animals, @digits); # just look ...
> 
> Consider: print Dumper( $animals, $digits );
> 
my @pets = ('dogs' , 'cats' , 'horses');
my @numbers = (1..10);
 
my_study (\@pets , \@numbers);

sub my_study {
my @animals = @{ shift @_ };
my @digits  = @{ shift @_ };

# code for sub my_study
}

-- 
Don't stop where the ink does.
Shawn

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




Re: How do I pass arrays into a subroutine

2013-09-07 Thread Dr.Ruud

On 07/09/2013 13:43, Karol Bujaček wrote:


   print Dumper(@_);# just look how the @_ looks like


Consider: print Dumper( \@_ );



   my($animals, $digits) = @_;

   my @animals = @$animals; # dereference
   my @digits = @$digits;   # dereference


The comment is inaccurate.

Why would you want to copy the arrays?



   print Dumper(@animals, @digits); # just look ...


Consider: print Dumper( $animals, $digits );

--
Ruud


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




Re: How do I pass arrays into a subroutine

2013-09-07 Thread Karol Bujaček

On 09/07/2013 01:11 PM, eventual wrote:

Hi,
In the example below, how do I pass @pets and @numbers into the
subroutine so that
@animals = @pets  and
@digits = @numbers.
Thanks
my @pets = ('dogs' , 'cats' , 'horses');
my @numbers = (1..10);
&study (@pets , @numbers);
sub study {
  my (@animals, @digits) = (@_[0] , @_[1]);
  print "Animals = @animals\n";
  print "Digits = @digits\n";
}


Hi,

You should use an array reference and dereference them in the subroutine.

Also, do not use 'study', as it is defined already 
<http://perldoc.perl.org/functions/study.html>.



Here is the code:


use strict;
use warnings;

use Data::Dumper::Simple;

my @pets = ('dogs', 'cats', 'horses');
my @numbers = (1..10);

my_sub(\@pets, \@numbers); # array references are passed as arguments

sub my_sub {
  print Dumper(@_);# just look how the @_ looks like

  my($animals, $digits) = @_;

  my @animals = @$animals; # dereference
  my @digits = @$digits;   # dereference

  print Dumper(@animals, @digits); # just look ...

  print "Animals = @animals\n";
  print "Digits = @digits\n";
}

Best regards,
Karol Bujacek


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




Re: How do I pass arrays into a subroutine

2013-09-07 Thread *Shaji Kalidasan*
Greetings,

You cannot pass two arrays as the previous array will slurp (consume) all the 
elements. You need to go for references.

Here is one way to do it

[code]
use strict;
use warnings;

my @pets = ('dogs' , 'cats' , 'horses');
my @numbers = (1..10);
 
&study (\@pets , \@numbers);
 
sub study {
 
     my ($animals, $digits) = @_;
 
     print "Animals = @$animals\n";
     print "Digits = @$digits\n";
}
[/code]

[output]
Animals = dogs cats horses
Digits = 1 2 3 4 5 6 7 8 9 10
[/output]
 
best,
Shaji 
---
Your talent is God's gift to you. What you do with it is your gift back to God.
---



 From: eventual 
To: "beginners@perl.org"  
Sent: Saturday, 7 September 2013 4:41 PM
Subject: How do I pass arrays into a subroutine
 


Hi,
 
In the example below, how do I pass @pets and @numbers into the subroutine so 
that
@animals = @pets  and 
@digits = @numbers.
Thanks
 
my @pets = ('dogs' , 'cats' , 'horses');
my @numbers = (1..10);
 
&study (@pets , @numbers);
 
sub study {
 
 my (@animals, @digits) = (@_[0] , @_[1]);
 
 print "Animals = @animals\n";
 print "Digits = @digits\n";
}

Re: How do I pass arrays into a subroutine

2013-09-07 Thread Shlomi Fish
Hi Eventual,

On Sat, 7 Sep 2013 04:11:06 -0700 (PDT)
eventual  wrote:

> Hi,
>  
> In the example below, how do I pass @pets and @numbers into the subroutine so
> that @animals = @pets  and 
> @digits = @numbers.

Please look into references and pass the arrays as references. See:

http://perl-begin.org/topics/references/

(NOTE: perl-begin.org is a site I originated and still maintain.).

Some comments about your code:

> Thanks
>  
> my @pets = ('dogs' , 'cats' , 'horses');
> my @numbers = (1..10);
>  
> &study (@pets , @numbers);

1. There's already a built-in function called study so please pick another name:

http://perldoc.perl.org/functions/study.html

2. Please don't call subroutines with ampersands ("&"):

http://perl-begin.org/tutorials/bad-elements/#ampersand-in-subroutine-calls

>  
> sub study {
>  
>  my (@animals, @digits) = (@_[0] , @_[1]);

1. Don't subscript arrays using @array[$idx]:

http://perl-begin.org/tutorials/bad-elements/#at-array-for-subscripting

2. Don't use positional arguments:

http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments

3. Assigning to two arrays will cause the first array to swallow all the
elements. Use references.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
http://www.shlomifish.org/humour/bits/Can-I-SCO-Now/ - "Can I SCO Now?"

Buffy Summers does not really need stakes to slay vampires, because her kisses
are deadly for them. And that includes those that she blows in the air.
— http://www.shlomifish.org/humour/bits/facts/Buffy/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




How do I pass arrays into a subroutine

2013-09-07 Thread eventual
Hi,
 
In the example below, how do I pass @pets and @numbers into the subroutine so 
that
@animals = @pets  and 
@digits = @numbers.
Thanks
 
my @pets = ('dogs' , 'cats' , 'horses');
my @numbers = (1..10);
 
&study (@pets , @numbers);
 
sub study {
 
 my (@animals, @digits) = (@_[0] , @_[1]);
 
 print "Animals = @animals\n";
 print "Digits = @digits\n";
}

Re: subroutine

2012-08-17 Thread Chris Stinemetz
> In the above line, `shift` will return just the first element from the
> @_ array. $y will therefore be undefined. The line should be rewritten
> as:
> my ( $x, $y ) = @_;
>
Thank you. I should have caught that.

Chris


Re: subroutine

2012-08-17 Thread Alan Haggai Alavi
Hello Chris,

> Can this subroutine be better written?
> I am getting the following erros:
>
> Use of uninitialized value $y in subtraction (-) at form.pl line 52.
> Use of uninitialized value $y in addition (+) at form.pl line 52.
>
> sub avg_az {
>   my($x, $y) = shift(@_);

In the above line, `shift` will return just the first element from the
@_ array. $y will therefore be undefined. The line should be rewritten
as:
my ( $x, $y ) = @_;

Relevant documentation: `perldoc -f shift` or
http://perldoc.perl.org/functions/shift.html

Regards,
Alan Haggai Alavi.
-- 
The difference makes the difference

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




subroutine

2012-08-17 Thread Chris Stinemetz
Hello List,
Can this subroutine be better written?
I am getting the following erros:

Use of uninitialized value $y in subtraction (-) at form.pl line 52.
Use of uninitialized value $y in addition (+) at form.pl line 52.

sub avg_az {
  my($x, $y) = shift(@_);
  return abs($x-$y)<180 ? ($x+$y)/2 :
  (360+$x+$y)/2<360 ? (360+$x+$y)/2 :
  (360+$x+$y)/2-360
}

print avg_az(90,30),"\n";

Thank you,

Chris


Re: subroutine in seperate file, question

2012-08-11 Thread Rob Dixon

On 11/08/2012 12:43, Shawn H Corey wrote:


No, @ISA is used only by Exporter.pm


Exporter makes no use of @ISA at all. This array is a per-package
variable used by Perl internally to implement object-oriented
inheritance. It is very often used to make a package a subclass of
Exporter, but that is by no means necessary.

Rob

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




Re: subroutine in seperate file, question

2012-08-11 Thread Rajeev Prasad
Thank you. I am confused about how to send variables to this ext.pl and get 
values (scalars) back into main.pl

another confusion is from web the cgi will run, i want to check cookies in 
external subroutine file and redirect to some website based on some condition, 
or gives some values back to calling program if condition is met.  can i do 
this kind of thing by putting this subroutine in external file? i am reading 
"perldoc perlmod" but still not able to understand the concept




 From: Shawn H Corey 
To: beginners@perl.org 
Sent: Saturday, August 11, 2012 6:43 AM
Subject: Re: subroutine in seperate file, question
 
On Sat, 11 Aug 2012 02:49:56 -0400
shawn wilson  wrote:

> On Aug 10, 2012 11:41 PM,  wrote:
> >
> > > I mean to ask, wether they will clash with the same loaded modules
> loaded
> > > in calling script?
> > >
> >
> > No. they are loaded only once.
> >
> 
> Well, they will both be in ISA to look up separately but there is no
> conflict.

No, @ISA is used only by Exporter.pm

It's %INC that records what modules have been loaded.

See `perldoc perlvar` and search for /%INC/


-- 
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

    _Perl links_
official site   : http://www.perl.org/
beginners' help : http://learn.perl.org/faq/beginners.html
advance help    : http://perlmonks.org/
documentation   : http://perldoc.perl.org/
news            : http://perlsphere.net/
repository      : http://www.cpan.org/
blog            : http://blogs.perl.org/
regional groups : http://www.pm.org/

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

Re: subroutine in seperate file, question

2012-08-11 Thread Shawn H Corey
On Sat, 11 Aug 2012 02:49:56 -0400
shawn wilson  wrote:

> On Aug 10, 2012 11:41 PM,  wrote:
> >
> > > I mean to ask, wether they will clash with the same loaded modules
> loaded
> > > in calling script?
> > >
> >
> > No. they are loaded only once.
> >
> 
> Well, they will both be in ISA to look up separately but there is no
> conflict.

No, @ISA is used only by Exporter.pm

It's %INC that records what modules have been loaded.

See `perldoc perlvar` and search for /%INC/


-- 
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

_Perl links_
official site   : http://www.perl.org/
beginners' help : http://learn.perl.org/faq/beginners.html
advance help: http://perlmonks.org/
documentation   : http://perldoc.perl.org/
news: http://perlsphere.net/
repository  : http://www.cpan.org/
blog: http://blogs.perl.org/
regional groups : http://www.pm.org/

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




Re: subroutine in seperate file, question

2012-08-10 Thread shawn wilson
On Aug 10, 2012 11:41 PM,  wrote:
>
> > I mean to ask, wether they will clash with the same loaded modules
loaded
> > in calling script?
> >
>
> No. they are loaded only once.
>

Well, they will both be in ISA to look up separately but there is no
conflict.


Re: subroutine in seperate file, question

2012-08-10 Thread pangj
> I mean to ask, wether they will clash with the same loaded modules loaded
> in calling script?
>

No. they are loaded only once.


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




Re: subroutine in seperate file, question

2012-08-10 Thread Rajeev Prasad
I mean to ask, wether they will clash with the same loaded modules loaded in 
calling script?




From: "pa...@riseup.net" 
To: Rajeev Prasad  
Cc: perl list  
Sent: Friday, August 10, 2012 9:05 PM
Subject: Re: subroutine in seperate file, question

Yes, the modules in required file will be loaded.

$ cat ext.pl
use CGI;
use DBI;

1;

$ cat main.pl
require 'ext.pl';
use Data::Dumper;

print Dumper \%INC;

Thus run perl main.pl to see what prints.


> i want to keep a peice of code which uses CGI and DBIx module in a
> seperate file, and want to include it in all my scripts as follows:
>  
> require "/path/to/script/file";
>  
> I am not sure if the calling program is also using same modules CGI and
> DBIx what kind of unknown errors i might get? anyone know?

Re: subroutine in seperate file, question

2012-08-10 Thread pangj
Yes, the modules in required file will be loaded.

$ cat ext.pl
use CGI;
use DBI;

1;

$ cat main.pl
require 'ext.pl';
use Data::Dumper;

print Dumper \%INC;

Thus run perl main.pl to see what prints.


> i want to keep a peice of code which uses CGI and DBIx module in a
> seperate file, and want to include it in all my scripts as follows:
>  
> require "/path/to/script/file";
>  
> I am not sure if the calling program is also using same modules CGI and
> DBIx what kind of unknown errors i might get? anyone know?


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




subroutine in seperate file, question

2012-08-10 Thread Rajeev Prasad
i want to keep a peice of code which uses CGI and DBIx module in a seperate 
file, and want to include it in all my scripts as follows:
 
require "/path/to/script/file";
 
I am not sure if the calling program is also using same modules CGI and DBIx 
what kind of unknown errors i might get? anyone know?
 
ty.
Rajeev

Re: array ref as subroutine parameter

2012-07-12 Thread timothy adigun
Hi Chris,

On Fri, Jul 13, 2012 at 3:28 AM, Shawn H Corey wrote:

> On 12-07-12 10:25 PM, Chris Stinemetz wrote:
>
>> I have an anonymous array below and would like to know how to pass the
>> first element to a subroutine as a parameter.
>>
>> push @data, [$srt,$srfc,$cfc,$cfcq,$cell,$**icell,$isector,$sector];
>>
>> call to subroutine:
>>
>> session_attempts($srt);
>>
>> Thank you in advance,
>>
>> Chris
>>
>>
> session_attempts( $data[0][0] );
>

OR

   session_attempts( $data[0]->[0] );
# though less readable and ofcourse you can do away with the arrow just
as Shawn showed previous


>
> --
> Just my 0.0002 million dollars worth,
>   Shawn
>
> Programming is as much about organization and communication
> as it is about coding.
>
> _Perl links_
> official site   : http://www.perl.org/
> beginners' help : 
> http://learn.perl.org/faq/**beginners.html<http://learn.perl.org/faq/beginners.html>
> advance help: http://perlmonks.org/
> documentation   : http://perldoc.perl.org/
> news: http://perlsphere.net/
> repository  : http://www.cpan.org/
> blog: http://blogs.perl.org/
> regional groups : http://www.pm.org/
>
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
Tim


Re: array ref as subroutine parameter

2012-07-12 Thread Shawn H Corey

On 12-07-12 10:25 PM, Chris Stinemetz wrote:

I have an anonymous array below and would like to know how to pass the
first element to a subroutine as a parameter.

push @data, [$srt,$srfc,$cfc,$cfcq,$cell,$icell,$isector,$sector];

call to subroutine:

session_attempts($srt);

Thank you in advance,

Chris



session_attempts( $data[0][0] );


--
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

_Perl links_
official site   : http://www.perl.org/
beginners' help : http://learn.perl.org/faq/beginners.html
advance help: http://perlmonks.org/
documentation   : http://perldoc.perl.org/
news: http://perlsphere.net/
repository  : http://www.cpan.org/
blog: http://blogs.perl.org/
regional groups : http://www.pm.org/



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




array ref as subroutine parameter

2012-07-12 Thread Chris Stinemetz
I have an anonymous array below and would like to know how to pass the
first element to a subroutine as a parameter.

push @data, [$srt,$srfc,$cfc,$cfcq,$cell,$icell,$isector,$sector];

call to subroutine:

session_attempts($srt);

Thank you in advance,

Chris


Re: subroutine returning data

2012-06-10 Thread Shlomi Fish
Hi all,

On Tue, 05 Jun 2012 10:15:36 -0700
"John W. Krahn"  wrote:

> [ Please do not top-post your replies.  Please remove non-relevant text 
> from your reply before posting.  TIA ]
> > don't insult and dismiss out of hand the findings of those who take the 
> > time to help you.
> 
> If I insulted Shlomi then I apologize.  But he was also just trying to 
> help "Chris Stinemetz".
> 

Just for the record, I do not recall being particularly insulted from John's
reply to my reply to his post. I mentioned some problems that I perceived in
his code, and he asked me why I think so. It wasn't uncivil or insulting.

So no need to apologise or to accuse John of being insulting.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
http://www.shlomifish.org/humour/ways_to_do_it.html

Had I not been already insane, I would have long ago driven myself mad.
— The Enemy and how I Helped to Fight It

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




Re: Unfamiliar calling of a subroutine

2012-06-08 Thread sono-io
On Jun 7, 2012, at 10:19 PM, Brock wrote:

> Finally to your last question, how is this different than just calling the 
> package::sub directly? With a method call the instance variable, in this case 
> $catalog, always gets passed as the first parameter to the method. Usually 
> people name it $self.

Thanks, Brock.  That is exactly what's happening in this script.  Your 
explanation helped clear things up for me.

> The same thing happens here -- so this is equivalent to:
> 
>  my $item = SurfDB::split_line($catalog, $itemid);


I'm reading through Intermediate Perl, as pangj suggested, but I have 
one other question.  If the above line is the same as writing:

 my $item = $catalog->SurfDB::split_line($itemid);

as you suggest, why use one over the other?  Is it just a matter of style, or 
is there more to this than meets the eye?

Thanks again,
Marc
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Unfamiliar calling of a subroutine

2012-06-07 Thread Brock
On 2012.06.07.20.21, sono...@fannullone.us wrote:
>   Would someone be so kind as to explain the following, or at least point 
> me to further reading?
> 
> $item = 
> $main::global->{open}->{catalog}->SurfDB::split_line($main::global->{form}->{'itemid'});
> 
>   The part I'm having trouble understanding is
>   "$main::global->{open}->{catalog}->".  What does that do to the 
> split_line
>   sub that's being called?  What does it do differently than just calling
>   SurfDB::split_line($main::global->{form}->{'itemid'}) by itself?

Good question. Let's break this into a few lines that are more-or-less 
equivalent:

  my $catalog = $main::global->{open}->{catalog};
  my $itemid  = $main::global->{form}->{'itemid'}

  my $item= $catalog->SurfDB::split_line($itemid);

So that is just doing what you did mentally, I think, and clarifies the problem.

If that line was instead:

  my $item = $catalog->split_line($itemid);

then this would be a straightforward perl object-oriented call. The $catalog 
would be a blessed variable (an instance variable), and split_line would be a 
sub (method) in the package (class) that $catalog was blessed with.

But instead we have a package name and sub spelled out explicitly, 
"SurfDB::split_line". So rather than perl using the package/class that $catalog 
belongs to it will use the SurfDB::split_line sub.

Finally to your last question, how is this different than just calling the 
package::sub directly? With a method call the instance variable, in this case 
$catalog, always gets passed as the first parameter to the method. Usually 
people name it $self. The same thing happens here -- so this is equivalent to:

  my $item = SurfDB::split_line($catalog, $itemid);

... I think :)

--Brock


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




Re: Unfamiliar calling of a subroutine

2012-06-07 Thread pangj



Would someone be so kind as to explain the following, or at least point 
me to further reading?

$item = 
$main::global->{open}->{catalog}->SurfDB::split_line($main::global->{form}->{'itemid'});



That's Perl OO programming.
You may want to read this book:
http://shop.oreilly.com/product/9780596102067.do

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




Unfamiliar calling of a subroutine

2012-06-07 Thread sono-io
Would someone be so kind as to explain the following, or at least point 
me to further reading?

$item = 
$main::global->{open}->{catalog}->SurfDB::split_line($main::global->{form}->{'itemid'});

The part I'm having trouble understanding is 
"$main::global->{open}->{catalog}->".  What does that do to the split_line sub 
that's being called?  What does it do differently than just calling 
SurfDB::split_line($main::global->{form}->{'itemid'}) by itself?

Thanks,
Marc
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: subroutine returning data

2012-06-05 Thread John W. Krahn
[ Please do not top-post your replies.  Please remove non-relevant text 
from your reply before posting.  TIA ]



Jack Maney wrote:


ProTip:


The top two results from Google state:

PROTIP | Know Your Meme
About PROTIP is a term often used in forums and comments to preface snarky,
obvious, counterintuitive, or sometimes genuine advice for the novice. 
Its usage.

knowyourmeme.com/memes/protip - Cached
#
Urban Dictionary: protip
Obvious advice sarcastically presented as sage wisdom.
www.urbandictionary.com/define.php?term=protip - Cached - Similar



If you are trying to imply that you are a professional then snarky or 
sarcastic comments do not bode well.




If you're going to ask for help,


I was providing code to "Chris Stinemetz" who *was* asking for help.



don't insult and dismiss out of hand the findings of those who take the time to 
help you.


If I insulted Shlomi then I apologize.  But he was also just trying to 
help "Chris Stinemetz".




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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




List behavior pro tips (was: Re: subroutine returning data)

2012-06-05 Thread John SJ Anderson
Even more pro tips: 

* Let the list moderator (ahem, that would be me) deal with policing the 
behavior of the list participants.  

* Realize that this list is publicly archived and indexed by search engines. 
Realize that potential employers often Google applicants. Realize your behavior 
here, as such, is "in public" and reflects on you for better or worse. 

* Seriously, LET THE LIST MODERATOR DEAL WITH POLICING BEHAVIOR. If you feel 
like you *absolutely* *must* *say* _SOMETHING_ -- say it directly to me. If it 
just can't wait for email, I'm on IRC on freenode and irc.perl; /msg genehack 
and I'll respond. Otherwise, if you find yourself responding, on-list, in a 
confrontational way, STOP. Just don't send that email. Please. 

* The whole thing with calling out other peoples responses is getting really 
old. If you think somebody gave a bad or uninformative response, the best way 
to deal with that is to write a better one, and explain why it's better, 
ideally without specifically referring to the other person's post. 

* Remember that we're here to help Perl beginners. We're not here to snipe at 
each others answers. That doesn't help beginners.  


Thanks to all the beginners asking questions, and all the people providing 
answers (and sorry for not being one of those people more often). You are all 
appreciated.

thanks,
john.


-- 
John SJ Anderson / geneh...@genehack.org 


On Tuesday, June 5, 2012 at 11:08 AM, Jack Maney wrote:

> ProTip: If you're going to ask for help, don't insult and dismiss out of hand 
> the findings of those who take the time to help you.
> 
> -Original Message-
> From: John W. Krahn [mailto:jwkr...@shaw.ca] 
> Sent: Tuesday, June 05, 2012 2:18 AM
> To: Perl Beginners
> Subject: Re: subroutine returning data
> 
> Shlomi Fish wrote:
> > 
> > On Mon, 04 Jun 2012 14:19:27 -0700
> > "John W. Krahn"mailto:jwkr...@shaw.ca)> wrote:
> > 
> > > Chris Stinemetz wrote:
> > > > I have a subroutine that I want to "return 1" only if the value of
> > > > %{$href->{$_[0]}} is equal to 'ND' for the whole 24 occurences.
> > > 
> > > 
> > > 
> > > One way to do it:
> > > 
> > > sub site_offAir {
> > > return values %{ $href->{ $_[ 0 ] } } == grep( $_ eq 'ND', values
> > > %{ $href->{ $_[ 0 ] } } ) ? 1 : '';
> > > }
> > 
> > 
> > 
> > I see several problems with your code:
> 
> You are entitled to your opinion.
> 
> > 1. It's quite hard to understand the logic of it.
> 
> No it isn't. (IMHO)
> 
> > 2. It won't stop when it encounter the first "ND" value.
> 
> That is true. And your point?
> 
> > 3. You have the values % { $href->{ $_[0] } } gob twice (a duplicate
> > expression).
> 
> 
> 
> Yes. And...
> 
> > 4. You've used $_[0] which is a positional parameter,
> 
> So?
> 
> > see:
> > http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments
> 
> 
> 
> Your argument on your web page does not appear to apply to this situation.
> 
> > 5. The grep does not uses braces for its predicate/block which is harder to
> > read.
> 
> 
> 
> In your opinion. I prefer to not use braces unless I have to.
> 
> > 6. You will return a true value (a list of length 1) when the function is
> > called in list context.
> 
> 
> 
> Yes, just as the OP's code.
> 
> 
> 
> 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-unsubscr...@perl.org 
> (mailto:beginners-unsubscr...@perl.org)
> For additional commands, e-mail: beginners-h...@perl.org 
> (mailto:beginners-h...@perl.org)
> http://learn.perl.org/




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




RE: subroutine returning data

2012-06-05 Thread Jack Maney
ProTip: If you're going to ask for help, don't insult and dismiss out of hand 
the findings of those who take the time to help you.

-Original Message-
From: John W. Krahn [mailto:jwkr...@shaw.ca] 
Sent: Tuesday, June 05, 2012 2:18 AM
To: Perl Beginners
Subject: Re: subroutine returning data

Shlomi Fish wrote:
>
> On Mon, 04 Jun 2012 14:19:27 -0700
> "John W. Krahn"  wrote:
>
>> Chris Stinemetz wrote:
>>> I have a subroutine that I want to "return 1" only if the value of
>>> %{$href->{$_[0]}} is equal to 'ND' for the whole 24 occurences.
>>
>> One way to do it:
>>
>> sub site_offAir {
>>   return values %{ $href->{ $_[ 0 ] } } == grep( $_ eq 'ND', values
>> %{ $href->{ $_[ 0 ] } } ) ? 1 : '';
>>   }
>>
>
> I see several problems with your code:

You are entitled to your opinion.

> 1. It's quite hard to understand the logic of it.

No it isn't.  (IMHO)

> 2. It won't stop when it encounter the first "ND" value.

That is true.  And your point?

> 3. You have the values % { $href->{ $_[0] } } gob twice (a duplicate
> expression).

Yes.  And...

> 4. You've used $_[0] which is a positional parameter,

So?

> see:
> http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments

Your argument on your web page does not appear to apply to this situation.

> 5. The grep does not uses braces for its predicate/block which is harder to
> read.

In your opinion.  I prefer to not use braces unless I have to.

> 6. You will return a true value (a list of length 1) when the function is
> called in list context.

Yes, just as the OP's code.



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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: subroutine returning data

2012-06-05 Thread John W. Krahn

Shlomi Fish wrote:


On Mon, 04 Jun 2012 14:19:27 -0700
"John W. Krahn"  wrote:


Chris Stinemetz wrote:

I have a subroutine that I want to "return 1" only if the value of
%{$href->{$_[0]}} is equal to 'ND' for the whole 24 occurences.


One way to do it:

sub site_offAir {
  return values %{ $href->{ $_[ 0 ] } } == grep( $_ eq 'ND', values
%{ $href->{ $_[ 0 ] } } ) ? 1 : '';
  }



I see several problems with your code:


You are entitled to your opinion.


1. It's quite hard to understand the logic of it.


No it isn't.  (IMHO)


2. It won't stop when it encounter the first "ND" value.


That is true.  And your point?


3. You have the values % { $href->{ $_[0] } } gob twice (a duplicate
expression).


Yes.  And...


4. You've used $_[0] which is a positional parameter,


So?


see:
http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments


Your argument on your web page does not appear to apply to this situation.


5. The grep does not uses braces for its predicate/block which is harder to
read.


In your opinion.  I prefer to not use braces unless I have to.


6. You will return a true value (a list of length 1) when the function is
called in list context.


Yes, just as the OP's code.



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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: subroutine returning data

2012-06-04 Thread Shlomi Fish
Hello John,

On Mon, 04 Jun 2012 14:19:27 -0700
"John W. Krahn"  wrote:

> Chris Stinemetz wrote:
> > I have a subroutine that I want to "return 1" only if the value of
> > %{$href->{$_[0]}} is equal to 'ND' for the whole 24 occurences.
> 
> 
> One way to do it:
> 
> sub site_offAir {
>  return values %{ $href->{ $_[ 0 ] } } == grep( $_ eq 'ND', values 
> %{ $href->{ $_[ 0 ] } } ) ? 1 : '';
>  }
> 

I see several problems with your code:

1. It's quite hard to understand the logic of it.

2. It won't stop when it encounter the first "ND" value.

3. You have the values % { $href->{ $_[0] } } gob twice (a duplicate
expression).

4. You've used $_[0] which is a positional parameter, see:

http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments

5. The grep does not uses braces for its predicate/block which is harder to
read.

6. You will return a true value (a list of length 1) when the function is
called in list context.



I prefer the suggestions in the previous sub-thread with the looping and
returning. Other ones can be written in a good manner using List::MoreUtils'
any/all/none/notall , and the smart match operator. 

While we are discussing bad solutions, another option is to do:

# Bad code - don't use
sub site_offAir
{
my ($hash_ref) = @_;
my @v = keys(reverse(%$hash_ref));
return ((@v == 1) and ($v[0] eq 'ND'));
}

But please don't use it.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang

 “We are NO LONGER the knights who say ‘BitKeeper’. We are now 
the knights who say ‘git, git, git, cogito — Linus!’.”

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




Re: subroutine returning data

2012-06-04 Thread John W. Krahn

Chris Stinemetz wrote:

I have a subroutine that I want to "return 1" only if the value of
%{$href->{$_[0]}} is equal to 'ND' for the whole 24 occurences.



One way to do it:

sub site_offAir {
return values %{ $href->{ $_[ 0 ] } } == grep( $_ eq 'ND', values 
%{ $href->{ $_[ 0 ] } } ) ? 1 : '';

}




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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: subroutine returning data

2012-06-04 Thread Chris Stinemetz
Thank you everyone. Your help has been very helpful..

Chris

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




Re: subroutine returning data

2012-06-04 Thread Shawn H Corey

On 12-06-04 02:01 PM, Bill Stephenson wrote:

The "values %{$href->{$_[0]}}"  code is pretty ugly but I get it now. And it 
make sense to break out of the loop as soon as you don't pass the test.


sub site_offAir {
  my $site_id = shift @_;

  for my $activity_code ( values %{ $activity_of->{$site_id} } ){
return if $activity_code ne 'ND';
  }

  return 1;

}


--
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

_Perl links_
official site   : http://www.perl.org/
beginners' help : http://learn.perl.org/faq/beginners.html
advance help: http://perlmonks.org/
documentation   : http://perldoc.perl.org/
news: http://perlsphere.net/
repository  : http://www.cpan.org/
blog: http://blogs.perl.org/
regional groups : http://www.pm.org/

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




Re: subroutine returning data

2012-06-04 Thread Bill Stephenson
Thanks Shawn!

The "values %{$href->{$_[0]}}"  code is pretty ugly but I get it now. And it 
make sense to break out of the loop as soon as you don't pass the test.

Kindest Regards,

Bill Stephenson



On Jun 4, 2012, at 12:49 PM, Shawn H Corey wrote:

> On 12-06-04 12:30 PM, Chris Stinemetz wrote:
>> I have a subroutine that I want to "return 1" only if the value of
>> %{$href->{$_[0]}} is equal to 'ND' for the whole 24 occurences.
>> 
>> Any suggestions is greatly appreciated.
>> 
>> Thank you,
>> 
>> Chris
>> 
>> sub site_offAir {
>>   for (values %{$href->{$_[0]}}) {
>> return 1 if $_ eq 'ND'; #need to test all values are eq to 'ND'
> 
>return if $_ ne 'ND';
> 
>>   }
>>   return '';
> 
>return 1;
> 
>> }
> 
> 
> -- 
> Just my 0.0002 million dollars worth,
>  Shawn
> 
> Programming is as much about organization and communication
> as it is about coding.
> 
>   _Perl links_
> official site   : http://www.perl.org/
> beginners' help : http://learn.perl.org/faq/beginners.html
> advance help: http://perlmonks.org/
> documentation   : http://perldoc.perl.org/
> news: http://perlsphere.net/
> repository  : http://www.cpan.org/
> blog: http://blogs.perl.org/
> regional groups : http://www.pm.org/
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 


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




Re: subroutine returning data

2012-06-04 Thread Shawn H Corey

On 12-06-04 12:30 PM, Chris Stinemetz wrote:

I have a subroutine that I want to "return 1" only if the value of
%{$href->{$_[0]}} is equal to 'ND' for the whole 24 occurences.

Any suggestions is greatly appreciated.

Thank you,

Chris

sub site_offAir {
   for (values %{$href->{$_[0]}}) {
 return 1 if $_ eq 'ND'; #need to test all values are eq to 'ND'


return if $_ ne 'ND';


   }
   return '';


return 1;


}



--
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

_Perl links_
official site   : http://www.perl.org/
beginners' help : http://learn.perl.org/faq/beginners.html
advance help: http://perlmonks.org/
documentation   : http://perldoc.perl.org/
news: http://perlsphere.net/
repository  : http://www.cpan.org/
blog: http://blogs.perl.org/
regional groups : http://www.pm.org/

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




Re: subroutine returning data

2012-06-04 Thread Bill Stephenson
On Jun 4, 2012, at 11:30 AM, Chris Stinemetz wrote:

> I have a subroutine that I want to "return 1" only if the value of
> %{$href->{$_[0]}} is equal to 'ND' for the whole 24 occurences.
> 
> Any suggestions is greatly appreciated.
> 

Chris, I don't know how to read your hash directly (hash of hashes?) but here's 
some logic that might help get the response you want,:

my %VAR1 = (
'00' => 'ND',
'01' => 'ND',
'02' => 'ND',
'10' => 'ND',
'03' => 'ND',
'11' => 'ND',
'20' => 'ND',
'04' => 'ND',
'12' => 'ND',
'21' => 'ND',
'05' => 'ND',
'13' => 'ND',
'22' => 'ND',
'06' => 'ND',
'14' => 'ND',
'23' => 'ND',
'07' => 'ND',
'15' => 'ND',
'08' => 'ND',
'16' => 'ND',
'09' => 'ND',
'17' => 'ND',
'18' => 'ND',
'19' => 'ND',
'19' => 'oD'
  );

print my $test = (&test_hash);

sub test_hash { 

my $return = 1;

while ( my($k,$v) = each %VAR1 ) {

if ($v ne 'ND') {

$return = 0;
}
}

return ($return);
}



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




Re: subroutine returning data

2012-06-04 Thread Paul Johnson
On Mon, Jun 04, 2012 at 11:30:30AM -0500, Chris Stinemetz wrote:
> I have a subroutine that I want to "return 1" only if the value of
> %{$href->{$_[0]}} is equal to 'ND' for the whole 24 occurences.
> 
> Any suggestions is greatly appreciated.
> 
> Thank you,
> 
> Chris
> 
> sub site_offAir {
>   for (values %{$href->{$_[0]}}) {
> return 1 if $_ eq 'ND'; #need to test all values are eq to 'ND'
>   }
>   return '';
> }

I would imagine it to be much easier to look at it from the other way.
Return 0 any time you find a value that does not equal "NO".  Then
return 1 at the end.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

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




subroutine returning data

2012-06-04 Thread Chris Stinemetz
I have a subroutine that I want to "return 1" only if the value of
%{$href->{$_[0]}} is equal to 'ND' for the whole 24 occurences.

Any suggestions is greatly appreciated.

Thank you,

Chris

sub site_offAir {
  for (values %{$href->{$_[0]}}) {
return 1 if $_ eq 'ND'; #need to test all values are eq to 'ND'
  }
  return '';
}


the call tot he sub-routine:
my @ND_wanted = grep site_offAir($_), sort  keys %$href;


an example from the data structure:

$VAR1 = {
  '077' => {
 '00' => 'ND',
 '01' => 'ND',
 '02' => 'ND',
 '10' => 'ND',
 '03' => 'ND',
 '11' => 'ND',
 '20' => 'ND',
 '04' => 'ND',
 '12' => 'ND',
 '21' => 'ND',
 '05' => 'ND',
 '13' => 'ND',
 '22' => 'ND',
 '06' => 'ND',
 '14' => 'ND',
 '23' => 'ND',
 '07' => 'ND',
 '15' => 'ND',
 '08' => 'ND',
 '16' => 'ND',
 '09' => 'ND',
 '17' => 'ND',
 '18' => 'ND',
 '19' => 'ND'
   }
};

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




Re: & and subroutine

2012-04-19 Thread Paul.G
Thanks for everyone's assistance, I think I have my answer. I will be looking 
closer at the comments.

cheers




 From: Michael Rasmussen 
To: beginners@perl.org 
Sent: Tuesday, 17 April 2012 11:01 PM
Subject: Re: & and subroutine
 
And for what it's worth.
113. Call subroutines with parentheses but without a leading &

That's from Damian Conway's Perl Best Practices
A quick reference to the 256 guidelines is found at
http://refcards.com/docs/vromansj/perl-best-practices/refguide.pdf

And a bit of luck, the entire chapter on subroutines, including the many 
reasons 
to prefer () to & is online:
http://www.devshed.com/c/a/Perl/Subroutines-in-Perl/


-- 
            Michael Rasmussen, Portland Oregon  
      Other Adventures: http://www.jamhome.us/ or http://westy.saunter.us/
Fortune Cookie Fortune du courrier:
The difference betwee a million and a billion is the difference between 
a sip of wine and 30 seconds with your daughter, and a bottle of gin and a 
night with her.
    ~ XKCD http://xkcd.com/558/

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

Re: & and subroutine

2012-04-17 Thread Uri Guttman

On 04/17/2012 08:30 AM, Manfred Lotz wrote:

On Tue, 17 Apr 2012 12:27:32 +0100
Gary Stainburn  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.


Perl tells you that it has no idea what you mean when you use the
bareword mysub.


Now you have two options to solve it.

1.&mysub;

Using the sigil&  you tell Perl that mysub is a subroutine.

2. mysub();

Usind () after mysub you also tell Perl that mysub is a subroutine.


3.&mysub();

Combining 1. and 2. works also but is not recommended. Hmm, at least I
do no recommend it.


Use 2. and you'll be happy.


There are surely some other situations where using&  might be
appropriate. But this one came into my mind immediately.


overall a good example. but it doesn't show where & might be 
appropriate. the () is correct and the & isn't. note that calling &mysub 
is different than calling &mysub() (or mysub()). that is the key reason 
to not use & for plain sub calls. as i said in my other post, i won't go 
into the reasons why as it is not important here. you just use () for 
sub calls and not &. even with code references $mysub->() is correct and 
&$mysub isn't.


thanx,

uri

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




Re: & and subroutine

2012-04-17 Thread Uri Guttman

On 04/17/2012 07:03 AM, 'lesleyb' wrote:

On Mon, Apr 16, 2012 at 04:20:35PM +0200, 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.


I'm tempted to say +1 for a really uninformative answer :)


well, it is actually a useful answer. i say things like you shouldn't 
use a certain feature (e.g. symrefs, string eval, etc.) until you know 
when to not use them. they are dangerous or obscure things which newbies 
may not get easily or quickly. explaining why can be difficult and not 
worth the immediate effort. it can be best to say don't and explain 
later on when they have more experience.




perldoc perlsub (http://perldoc.perl.org/perlsub.html) explains the '&' is
entirely optional - except where it isn't - e.g. where you might need to check
if the subroutine exists, use a reference to a subroutine rather than a direct
call etc.

There are provisos to the use of the '&' prefix and the documentation explains
those.


you see, that was not helpful. there are negatives (not just provisos) 
to using & for sub calls. you didn't state them but only paraphrased the 
docs. if you think the previous comment was not good, yours is worse as 
it makes it seem like using & is up to the coder ("entirely optional"). 
it isn't. it is bad coding in several ways. it isn't needed, it marks 
the code as perl4ish or newbie, and it has those downsides that i won't 
mention but which are documented. the rule is simple and doesn't need 
need deep explanation. you don't use & for plain sub calls. period.


thanx,

uri

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




Re: & and subroutine

2012-04-17 Thread Ken Slater
On Tue, Apr 17, 2012 at 9:43 AM, Gary Stainburn
 wrote:
> On Tuesday 17 April 2012 13:30:59 Manfred Lotz wrote:
>>
>> 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.
>>
>>
>> Perl tells you that it has no idea what you mean when you use the
>> bareword mysub.
>>
>>
>> Now you have two options to solve it.
>>
>> 1. &mysub;
>>
>> Using the sigil & you tell Perl that mysub is a subroutine.
>>
>> 2. mysub();
>>
>> Usind () after mysub you also tell Perl that mysub is a subroutine.
>>
>>
>> 3. &mysub();
>>
>> Combining 1. and 2. works also but is not recommended. Hmm, at least I
>> do no recommend it.
>>
>>
>> Use 2. and you'll be happy.
>>
>>
>> There are surely some other situations where using & might be
>> appropriate. But this one came into my mind immediately.
>>Hi Manfred,
>
> Thank you for this, but below is what I am meaning.
>
> Gary
>
> [root@stan ~]# cat t.pl
> #!/usr/bin/perl -w
>
> use strict;
> use warnings;
>
>
> mysub();
>
>
> sub mysub() {
>  print "hello world\n";
> }
> [root@stan ~]# ./t.pl
> main::mysub() called too early to check prototype at ./t.pl line 7.
> hello world
>
> }
>
> --
> Gary Stainburn
> Group I.T. Manager
> Ringways Garages
> http://www.ringways.co.uk
>

You are forcing the procedure mysub to use a prototype by including
the empty parameter list. You usually do not want to use prototypes.

Just define mysub without a prototype:

sub mysub {
  print "hello world\n";
}

HTH, Ken

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




Re: & and subroutine

2012-04-17 Thread Gary Stainburn
On Tuesday 17 April 2012 15:13:40 Manfred Lotz wrote:
> You have
> sub mysub() {
>
> instead of
>
> sub mysub {
>
> which is the correct way.

Thank you. I can't believe how many years I've been getting that one wrong.

Gary

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




Re: & and subroutine

2012-04-17 Thread Manfred Lotz
On Tue, 17 Apr 2012 14:43:01 +0100
Gary Stainburn  wrote:

> On Tuesday 17 April 2012 13:30:59 Manfred Lotz wrote:
> >
> > 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.
> >
> >
> > Perl tells you that it has no idea what you mean when you use the
> > bareword mysub.
> >
> >
> > Now you have two options to solve it.
> >
> > 1. &mysub;
> >
> > Using the sigil & you tell Perl that mysub is a subroutine.
> >
> > 2. mysub();
> >
> > Usind () after mysub you also tell Perl that mysub is a subroutine.
> >
> >
> > 3. &mysub();
> >
> > Combining 1. and 2. works also but is not recommended. Hmm, at
> > least I do no recommend it.
> >
> >
> > Use 2. and you'll be happy.
> >
> >
> > There are surely some other situations where using & might be
> > appropriate. But this one came into my mind immediately.
> >Hi Manfred,
> 
> Thank you for this, but below is what I am meaning.
> 
> Gary
> 
> [root@stan ~]# cat t.pl 
> #!/usr/bin/perl -w
> 
> use strict;
> use warnings;
> 
> 
> mysub();
> 
> 
> sub mysub() {
>   print "hello world\n";
> }
> [root@stan ~]# ./t.pl 
> main::mysub() called too early to check prototype at ./t.pl line 7.
> hello world
> 
> }
> 

You have
sub mysub() {

instead of

sub mysub {

which is the correct way.

--
Manfred



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




Re: & and subroutine

2012-04-17 Thread Gary Stainburn
On Tuesday 17 April 2012 13:30:59 Manfred Lotz wrote:
>
> 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.
>
>
> Perl tells you that it has no idea what you mean when you use the
> bareword mysub.
>
>
> Now you have two options to solve it.
>
> 1. &mysub;
>
> Using the sigil & you tell Perl that mysub is a subroutine.
>
> 2. mysub();
>
> Usind () after mysub you also tell Perl that mysub is a subroutine.
>
>
> 3. &mysub();
>
> Combining 1. and 2. works also but is not recommended. Hmm, at least I
> do no recommend it.
>
>
> Use 2. and you'll be happy.
>
>
> There are surely some other situations where using & might be
> appropriate. But this one came into my mind immediately.
>Hi Manfred,

Thank you for this, but below is what I am meaning.

Gary

[root@stan ~]# cat t.pl 
#!/usr/bin/perl -w

use strict;
use warnings;


mysub();


sub mysub() {
  print "hello world\n";
}
[root@stan ~]# ./t.pl 
main::mysub() called too early to check prototype at ./t.pl line 7.
hello world

}

>
> --
> Manfred



-- 
Gary Stainburn
Group I.T. Manager
Ringways Garages
http://www.ringways.co.uk 

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




Re: & and subroutine

2012-04-17 Thread Michael Rasmussen
And for what it's worth.
113. Call subroutines with parentheses but without a leading &

That's from Damian Conway's Perl Best Practices
A quick reference to the 256 guidelines is found at
http://refcards.com/docs/vromansj/perl-best-practices/refguide.pdf

And a bit of luck, the entire chapter on subroutines, including the many 
reasons 
to prefer () to & is online:
http://www.devshed.com/c/a/Perl/Subroutines-in-Perl/


-- 
Michael Rasmussen, Portland Oregon  
  Other Adventures: http://www.jamhome.us/ or http://westy.saunter.us/
Fortune Cookie Fortune du courrier:
The difference betwee a million and a billion is the difference between 
a sip of wine and 30 seconds with your daughter, and a bottle of gin and a 
night with her.
~ XKCD http://xkcd.com/558/

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




Re: & and subroutine

2012-04-17 Thread Michael Rasmussen
On Tue, Apr 17, 2012 at 02:30:59PM +0200, Manfred Lotz wrote:
> > 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.
 
Alternatively, and to my sensibilities cleaner:

#!/usr/bin/perl
use strict;
use warnings;

sub mysub;  #  declares it, mysub no longer a bareword

mysub();#  does it, () not required for Perl parser, but are nice for 
maintainers

sub mysub { # defines it
  print "Hi!  Nice to see you!\n";
}

-- 
Michael Rasmussen, Portland Oregon  
  Other Adventures: http://www.jamhome.us/ or http://westy.saunter.us/
Fortune Cookie Fortune du courrier:
Whatever destiny your relationship has, a tandem will get you there faster.

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




Re: & and subroutine

2012-04-17 Thread Shawn H Corey

On 12-04-17 08:30 AM, Manfred Lotz wrote:

On Tue, 17 Apr 2012 12:27:32 +0100
Gary Stainburn  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.0002 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/




Re: & and subroutine

2012-04-17 Thread Manfred Lotz
On Tue, 17 Apr 2012 12:27:32 +0100
Gary Stainburn  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.


Perl tells you that it has no idea what you mean when you use the
bareword mysub.


Now you have two options to solve it.

1. &mysub;

Using the sigil & you tell Perl that mysub is a subroutine.

2. mysub();

Usind () after mysub you also tell Perl that mysub is a subroutine.


3. &mysub();

Combining 1. and 2. works also but is not recommended. Hmm, at least I
do no recommend it.


Use 2. and you'll be happy.


There are surely some other situations where using & might be
appropriate. But this one came into my mind immediately.


-- 
Manfred









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




Re: & and subroutine

2012-04-17 Thread Gary Stainburn
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.

-- 
Gary Stainburn
Group I.T. Manager
Ringways Garages
http://www.ringways.co.uk 

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




Re: & and subroutine

2012-04-17 Thread 'lesleyb'
On Mon, Apr 16, 2012 at 04:20:35PM +0200, 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.
> 
I'm tempted to say +1 for a really uninformative answer :)

perldoc perlsub (http://perldoc.perl.org/perlsub.html) explains the '&' is
entirely optional - except where it isn't - e.g. where you might need to check
if the subroutine exists, use a reference to a subroutine rather than a direct
call etc.

There are provisos to the use of the '&' prefix and the documentation explains
those.

Regards 

Lesley

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




Re: & and subroutine

2012-04-16 Thread Paul Johnson
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.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

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




& and subroutine

2012-04-16 Thread Paul.G
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?


eg:

sub name {

some code here, returning a single value

return 0;

}

&name();

cheers


Re: subroutine call

2011-12-05 Thread Shlomi Fish
Hi Ganesh,

On Mon, 5 Dec 2011 14:16:29 +0530
ganesh vignesh  wrote:

> stop mail to me

The instructions at the bottom of every E-mail read:

[QUOTE]
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/
[/QUOTE]

Please follow them and you'll stop receiving E-mails. Sorry to see you
unsubscribe.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Rethinking CPAN - http://shlom.in/rethinking-cpan

“Interesting” has a negative correlation with “successful”.
— Anno on Freenode's #perl

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




Re: subroutine call

2011-12-05 Thread Shlomi Fish
Hello, John.

On Sun, 04 Dec 2011 12:46:40 -0800
"John W. Krahn"  wrote:

> > You should assign $marketInfo{$mkt} to a variable, or alternatively do:
> >
> > my ($start, $end) = @{$marketInfo{$mkt}}{qw(start end)};
> >
> >> my $end = $marketInfo{$mkt}->{"end"};
> >> if( $cell>= $start&&  $cell<= $end)
> >> {
> >>$_mkt = $mkt;
> >>last;
> >> }
> >>  }
> >
> > This loop can be more succinctly expressed using first from List::Util :
> >
> > return List::Util::first {
> > my ($start, $end) = @{$marketInfo{$_}}{qw(start end)};
> > $cell>= $start&&  $cell<= $end;
> > } keys(%marketInfo);
> >
> > All that put aside, this lookup has O(N) complexity, and you can reduce it 
> > to
> > O(log(N)) by creating a
> 
> > sorted array of ranges that you will binary search.
> ^
> O( log N )+  O( log N )
> 
> Or O( log 2N )

First of all, sorting an array is optimally O(N*log(N)) - not
O(log(N)). However, sorting this array is done *once* and then it is used for
all lookups, which means the total complexity should be divided by the number
of lookups. 

Furthermore, O(log(2N)) = O(log(2) + log(N)) = O(log(N)).

> 
> While the added efficiency MAY be faster it adds more complexity and it 
> depends on the size of N and the algorithms implementing sort and search 
> as to whether it will actually be faster.

That's true. But if N is very large, it's likely to be significant.

> 
> 
> We should forget about small efficiencies, say about 97% of the time: 
> premature optimization is the root of all evil
> -- Donald Knuth
> 

And ignoring asymptotic complexity is the opposite of a “small efficiency”. For
example, Visual Basic Classic implemented arrays as linked lists, which made
then practically unusable for random lookup: (i.e doing something like
myArray[i] where i is arbitrary). See:

http://tech.groups.yahoo.com/group/hackers-il/message/433

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
UNIX Fortune Cookies - http://www.shlomifish.org/humour/fortunes/

XSLT is what Chuck Norris has nightmares of.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




Re: subroutine call

2011-12-04 Thread John W. Krahn

Shlomi Fish wrote:


in addition to the good advice that Uri gave you - some comments on your code.

On Sun, 4 Dec 2011 07:40:09 -0600
Chris Stinemetz  wrote:


I have a program that I am working on improveing. The fist step I have
taken is converting it in using the strict pragma.

Now when this subroutine call is made I get the following compilation error:

Global symbol "$cell" requires explicit package name at ./evdo.pl line 279.
Global symbol "$cell" requires explicit package name at ./evdo.pl line 279.

I understand lexical scope, but am having a hard time figuring out how to
fix this.

Any help is greatly apprciated.

Chris

sub getMarket
{
 my $_cell = shift;
 my $_mkt = "";
 my $mkt = "";
 foreach $mkt (keys %marketInfo)
 {


This is better written using «foreach my $mkt» instead of «foreach $mkt»


my $start = $marketInfo{$mkt}->{"start"};


You should assign $marketInfo{$mkt} to a variable, or alternatively do:

my ($start, $end) = @{$marketInfo{$mkt}}{qw(start end)};


my $end = $marketInfo{$mkt}->{"end"};
if( $cell>= $start&&  $cell<= $end)
{
   $_mkt = $mkt;
   last;
}
 }


This loop can be more succinctly expressed using first from List::Util :

return List::Util::first {
my ($start, $end) = @{$marketInfo{$_}}{qw(start end)};
$cell>= $start&&  $cell<= $end;
} keys(%marketInfo);

All that put aside, this lookup has O(N) complexity, and you can reduce it to
O(log(N)) by creating a



sorted array of ranges that you will binary search.

   ^
   O( log N )+  O( log N )

Or O( log 2N )

While the added efficiency MAY be faster it adds more complexity and it 
depends on the size of N and the algorithms implementing sort and search 
as to whether it will actually be faster.



We should forget about small efficiencies, say about 97% of the time: 
premature optimization is the root of all evil

-- Donald Knuth



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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: subroutine call

2011-12-04 Thread Dr.Ruud

On 2011-12-04 18:12, Shlomi Fish wrote:

Chris wrote:



my $cell = substr($market,0,index($market,"_"));


  print "$_ <", substr( $_, 0, index $_, "_" ), ">\n"
for qw/ foo1 foo2_bar foo3_bar_baz /;

foo1 
foo2_bar 
foo3_bar_baz 


This can be more idiomatically (and more briefly) done using:

  (my $cell = $market) =~ s/_.*//s;


  print("$_ <"), (local $_ = $_) =~ s/_.*//s, print("$_>\n")
for qw/ foo1 foo2_bar foo3_bar_baz /;

foo1 
foo2_bar 
foo3_bar_baz 


Next alternative: my ($cell) = $market =~ s/([^_]*)/;

  print("$_ <"), local($_) = /([^_]*)/, print("$_>\n")
for qw/ foo1 foo2_bar foo3_bar_baz /;

foo1 
foo2_bar 
foo3_bar_baz 


But do realize that 'foo1' becomes 'foo' and not 'foo1', in the original 
code.


--
Ruud

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




Re: subroutine call

2011-12-04 Thread Shlomi Fish
Hi Chris,

in addition to the good advice that Uri gave you - some comments on your code.

On Sun, 4 Dec 2011 07:40:09 -0600
Chris Stinemetz  wrote:

> I have a program that I am working on improveing. The fist step I have
> taken is converting it in using the strict pragma.
> 
> Now when this subroutine call is made I get the following compilation error:
> 
> Global symbol "$cell" requires explicit package name at ./evdo.pl line 279.
> Global symbol "$cell" requires explicit package name at ./evdo.pl line 279.
> 
> I understand lexical scope, but am having a hard time figuring out how to
> fix this.
> 
> Any help is greatly apprciated.
> 
> Chris
> 
> sub getMarket
> {
> my $_cell = shift;
> my $_mkt = "";
> my $mkt = "";
> foreach $mkt (keys %marketInfo)
> {

This is better written using «foreach my $mkt» instead of «foreach $mkt»

>my $start = $marketInfo{$mkt}->{"start"};

You should assign $marketInfo{$mkt} to a variable, or alternatively do:

my ($start, $end) = @{$marketInfo{$mkt}}{qw(start end)};  

>my $end = $marketInfo{$mkt}->{"end"};
>if( $cell >= $start && $cell <= $end)
>{
>   $_mkt = $mkt;
>   last;
>}
> }

This loop can be more succinctly expressed using first from List::Util :

return List::Util::first { 
my ($start, $end) = @{$marketInfo{$_}}{qw(start end)};
$cell >= $start && $cell <= $end;
} keys(%marketInfo); 

All that put aside, this lookup has O(N) complexity, and you can reduce it to
O(log(N)) by creating a sorted array of ranges that you will binary search. 
> return $_mkt;
> }
> 
> 
> This is the foreach loop that calls the subroutine
> 
> foreach my $c_s (sort num keys %evdo)
> {
>my $market = $c_s;

Why are you using both $c_s and $market to iterate over the loop? The «my $c_s»
is already lexical and safe.

>my $satt = $evdo{$market}{$pegs[0]};
>my $sest = $evdo{$market}{$pegs[1]};
>my $fit = $evdo{$market}{$pegs[2]};
>my $psEst = $evdo{$market}{$pegs[3]};
>my $catt = $evdo{$market}{$pegs[4]};
>my $cest = $evdo{$market}{$pegs[5]};
>my $pcEst = $evdo{$market}{$pegs[6]};
>my $rfLost = $evdo{$market}{$pegs[7]};
>my $cpDropCell = $evdo{$market}{$pegs[8]};
>my $cpDropRnc = $evdo{$market}{$pegs[9]};
>my $tuneAway = $evdo{$market}{$pegs[10]};
>my $tDrops = $evdo{$market}{$pegs[11]};
>my $pDcr = $evdo{$market}{$pegs[12]};
>my $ia = $evdo{$market}{$pegs[13]};
>my $pIa = $evdo{$market}{$pegs[14]};
>my $tccf = $evdo{$market}{$pegs[15]};
>my $failAp = $evdo{$market}{$pegs[16]};
>my $failTp = $evdo{$market}{$pegs[17]};
>my $failA10 = $evdo{$market}{$pegs[18]};
>my $failAAA = $evdo{$market}{$pegs[19]};
>my $failPDSN = $evdo{$market}{$pegs[20]};

Wow! Duplicate code central! And it's also susceptible to index mismatch.
What I suggest you to do would be to have the $evdo{$market} be an object and
then you can work on its fields/slots and methods. See:

http://perl-begin.org/topics/object-oriented/

>my $cell = substr($market,0,index($market,"_"));

This can be more idiomatically (and more briefly) done using:

 (my $cell = $market) =~ s/_.*//s;

>my $mkt = &getMarket($cell);

Don't use leading ampersands in subroutine calls:

* http://perl-begin.org/tutorials/bad-elements/#ampersand-in-subroutine-calls

* https://www.socialtext.net/perl5/subroutines_called_with_the_ampersand  (Page
  requires JavaScript).

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

Chuck Norris does not code; when he sits at a computer, it just does whatever
he wants. (By: Kattana.)

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




Re: subroutine call

2011-12-04 Thread Uri Guttman

On 12/04/2011 08:40 AM, Chris Stinemetz wrote:

I have a program that I am working on improveing. The fist step I have
taken is converting it in using the strict pragma.

Now when this subroutine call is made I get the following compilation error:

Global symbol "$cell" requires explicit package name at ./evdo.pl line 279.
Global symbol "$cell" requires explicit package name at ./evdo.pl line 279.

I understand lexical scope, but am having a hard time figuring out how to
fix this.

Any help is greatly apprciated.

Chris

sub getMarket
{
 my $_cell = shift;


why are you putting a _ before the name? that is not a typical perl way 
to spell a scalar. _ before a sub name is a convention that the 
sub/method is private. that isn't needed with lexically scoped vars.




if( $cell>= $start&&  $cell<= $end)


look at the spelling there.

also that line (and others) can use more whitespace. it helps others to 
read the code.



uri


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




subroutine call

2011-12-04 Thread Chris Stinemetz
I have a program that I am working on improveing. The fist step I have
taken is converting it in using the strict pragma.

Now when this subroutine call is made I get the following compilation error:

Global symbol "$cell" requires explicit package name at ./evdo.pl line 279.
Global symbol "$cell" requires explicit package name at ./evdo.pl line 279.

I understand lexical scope, but am having a hard time figuring out how to
fix this.

Any help is greatly apprciated.

Chris

sub getMarket
{
my $_cell = shift;
my $_mkt = "";
my $mkt = "";
foreach $mkt (keys %marketInfo)
{
   my $start = $marketInfo{$mkt}->{"start"};
   my $end = $marketInfo{$mkt}->{"end"};
   if( $cell >= $start && $cell <= $end)
   {
  $_mkt = $mkt;
  last;
   }
}
return $_mkt;
}


This is the foreach loop that calls the subroutine

foreach my $c_s (sort num keys %evdo)
{
   my $market = $c_s;
   my $satt = $evdo{$market}{$pegs[0]};
   my $sest = $evdo{$market}{$pegs[1]};
   my $fit = $evdo{$market}{$pegs[2]};
   my $psEst = $evdo{$market}{$pegs[3]};
   my $catt = $evdo{$market}{$pegs[4]};
   my $cest = $evdo{$market}{$pegs[5]};
   my $pcEst = $evdo{$market}{$pegs[6]};
   my $rfLost = $evdo{$market}{$pegs[7]};
   my $cpDropCell = $evdo{$market}{$pegs[8]};
   my $cpDropRnc = $evdo{$market}{$pegs[9]};
   my $tuneAway = $evdo{$market}{$pegs[10]};
   my $tDrops = $evdo{$market}{$pegs[11]};
   my $pDcr = $evdo{$market}{$pegs[12]};
   my $ia = $evdo{$market}{$pegs[13]};
   my $pIa = $evdo{$market}{$pegs[14]};
   my $tccf = $evdo{$market}{$pegs[15]};
   my $failAp = $evdo{$market}{$pegs[16]};
   my $failTp = $evdo{$market}{$pegs[17]};
   my $failA10 = $evdo{$market}{$pegs[18]};
   my $failAAA = $evdo{$market}{$pegs[19]};
   my $failPDSN = $evdo{$market}{$pegs[20]};
   my $cell = substr($market,0,index($market,"_"));
   my $mkt = &getMarket($cell);
}


Re: timings of perl subroutine with a program

2011-11-07 Thread a b
Thanks a ton!!

Hats off to you for encouragement

On Fri, Nov 4, 2011 at 5:05 PM, Shlomi Fish  wrote:

> Hi "a b",
>
> On Fri, 4 Nov 2011 15:18:00 +0530
> a b  wrote:
>
> > apologize!!
> >
> > Can you help me to understand how async I/O can help me
> >
>
> What's wrong with the resources in the URL I pointed you to? There's also
> http://en.wikipedia.org/wiki/Asynchronous_I/O which may be of interest.
> "The Gods help them that help themselves." (
> http://en.wikiquote.org/wiki/Aesop ).
>
> Regards,
>
>Shlomi Fish
>
> > Regards
> > a b
> >
> > On Thu, Nov 3, 2011 at 9:06 PM, Shlomi Fish 
> wrote:
> >
> > > Hello a b,
> > >
> > > please reply to the list as I specifically request in my signature.
> > > (Wretched
> > > gmail.com.) I'm CCing the list.
> > >
> > > On Thu, 3 Nov 2011 16:15:11 +0530
> > > a b  wrote:
> > >
> > > > Thanks Shlomi!!
> > > >
> > > > I am not sure about async I/O?
> > > >
> > > > any pointers about this one. It is new to me so far
> > > >
> > >
> > > See the links from this URL:
> > >
> > > http://perl-begin.org/uses/multitasking/
> > >
> > > Regards,
> > >
> > >Shlomi Fish
> > >
> > > >
> > > > Regards
> > > > ~
> > > >
> > > > On 11/3/11, Shlomi Fish  wrote:
> > > > > On Thu, 3 Nov 2011 06:49:36 +0100
> > > > > timothy adigun <2teezp...@gmail.com> wrote:
> > > > >
> > > > >> Hi a b,
> > > > >>
> > > > >> a b  wrote:
> > > > >>
> > > > >> > Hi all,
> > > > >> >
> > > > >> > i need to track down how much time each function is taking and
> > > anlyze if
> > > > >> > threads can help
> > > > >> >
> > > > >> > do we have any such function??
> > > > >> >
> > > > >>
> > > > >> **You can use ** use Benchmark qw(:all) **.
> > > > >> From your CLI you can do: perldoc benchmark,
> > > > >> or if you not like reading from the command Line Interface, you
> can
> > > do:
> > > > >> perldoc -oHTML -dbenchmark.html benchmark,
> > > > >> then you have your perldoc benchmark in html format then read how
> to
> > > use
> > > > >> it --
> > > > >>
> > > > >
> > > > > In addition, see:
> > > > >
> > > > > http://search.cpan.org/dist/Devel-NYTProf/
> > > > >
> > > > > Which is a sophisticated profiler for Perl.
> > > > >
> > > > > Other than that, threads may not be the answer due to the way they
> are
> > > > > implemented in Perl. Process forking or async IO may be better:
> > > > >
> > > > > http://perl-begin.org/uses/multitasking/
> > > > >
> > > > > Regards,
> > > > >
> > > > > Shlomi Fish
> > > > >
> > > > > --
> > > > > -
> > > > > Shlomi Fish   http://www.shlomifish.org/
> > > > > "The Human Hacking Field Guide" - http://shlom.in/hhfg
> > > > >
> > > > > Comedy is simply a funny way of being serious.
> > > > > — http://en.wikiquote.org/wiki/Peter_Ustinov
> > > > >
> > > > > Please reply to list if it's a mailing list post -
> > > http://shlom.in/reply .
> > > > >
> > >
> > >
> > >
> > > --
> > > -
> > > Shlomi Fish   http://www.shlomifish.org/
> > > My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/
> > >
> > > “You are banished! You are banished! You are banished!
> > > Hey! I’m just kidding!”
> > >
> > > Please reply to list if it's a mailing list post -
> http://shlom.in/reply .
> > >
>
>
>
> --
> -
> Shlomi Fish   http://www.shlomifish.org/
> "The Human Hacking Field Guide" - http://shlom.in/hhfg
>
> When Chuck Norris uses git, he takes a coffee break after initiating every
> git
> commit. And then he waits for the commit to finish.
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
>


Re: timings of perl subroutine with a program

2011-11-04 Thread Shlomi Fish
Hi "a b",

On Fri, 4 Nov 2011 15:18:00 +0530
a b  wrote:

> apologize!!
> 
> Can you help me to understand how async I/O can help me
> 

What's wrong with the resources in the URL I pointed you to? There's also
http://en.wikipedia.org/wiki/Asynchronous_I/O which may be of interest. "The 
Gods help them that help themselves." ( http://en.wikiquote.org/wiki/Aesop ).

Regards,

Shlomi Fish

> Regards
> a b
> 
> On Thu, Nov 3, 2011 at 9:06 PM, Shlomi Fish  wrote:
> 
> > Hello a b,
> >
> > please reply to the list as I specifically request in my signature.
> > (Wretched
> > gmail.com.) I'm CCing the list.
> >
> > On Thu, 3 Nov 2011 16:15:11 +0530
> > a b  wrote:
> >
> > > Thanks Shlomi!!
> > >
> > > I am not sure about async I/O?
> > >
> > > any pointers about this one. It is new to me so far
> > >
> >
> > See the links from this URL:
> >
> > http://perl-begin.org/uses/multitasking/
> >
> > Regards,
> >
> >Shlomi Fish
> >
> > >
> > > Regards
> > > ~
> > >
> > > On 11/3/11, Shlomi Fish  wrote:
> > > > On Thu, 3 Nov 2011 06:49:36 +0100
> > > > timothy adigun <2teezp...@gmail.com> wrote:
> > > >
> > > >> Hi a b,
> > > >>
> > > >> a b  wrote:
> > > >>
> > > >> > Hi all,
> > > >> >
> > > >> > i need to track down how much time each function is taking and
> > anlyze if
> > > >> > threads can help
> > > >> >
> > > >> > do we have any such function??
> > > >> >
> > > >>
> > > >> **You can use ** use Benchmark qw(:all) **.
> > > >> From your CLI you can do: perldoc benchmark,
> > > >> or if you not like reading from the command Line Interface, you can
> > do:
> > > >> perldoc -oHTML -dbenchmark.html benchmark,
> > > >> then you have your perldoc benchmark in html format then read how to
> > use
> > > >> it --
> > > >>
> > > >
> > > > In addition, see:
> > > >
> > > > http://search.cpan.org/dist/Devel-NYTProf/
> > > >
> > > > Which is a sophisticated profiler for Perl.
> > > >
> > > > Other than that, threads may not be the answer due to the way they are
> > > > implemented in Perl. Process forking or async IO may be better:
> > > >
> > > > http://perl-begin.org/uses/multitasking/
> > > >
> > > > Regards,
> > > >
> > > > Shlomi Fish
> > > >
> > > > --
> > > > -
> > > > Shlomi Fish   http://www.shlomifish.org/
> > > > "The Human Hacking Field Guide" - http://shlom.in/hhfg
> > > >
> > > > Comedy is simply a funny way of being serious.
> > > > — http://en.wikiquote.org/wiki/Peter_Ustinov
> > > >
> > > > Please reply to list if it's a mailing list post -
> > http://shlom.in/reply .
> > > >
> >
> >
> >
> > --
> > -
> > Shlomi Fish   http://www.shlomifish.org/
> > My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/
> >
> > “You are banished! You are banished! You are banished!
> > Hey! I’m just kidding!”
> >
> > Please reply to list if it's a mailing list post - http://shlom.in/reply .
> >



-- 
-
Shlomi Fish   http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://shlom.in/hhfg

When Chuck Norris uses git, he takes a coffee break after initiating every git 
commit. And then he waits for the commit to finish.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




Re: timings of perl subroutine with a program

2011-11-04 Thread a b
apologize!!

Can you help me to understand how async I/O can help me

Regards
a b

On Thu, Nov 3, 2011 at 9:06 PM, Shlomi Fish  wrote:

> Hello a b,
>
> please reply to the list as I specifically request in my signature.
> (Wretched
> gmail.com.) I'm CCing the list.
>
> On Thu, 3 Nov 2011 16:15:11 +0530
> a b  wrote:
>
> > Thanks Shlomi!!
> >
> > I am not sure about async I/O?
> >
> > any pointers about this one. It is new to me so far
> >
>
> See the links from this URL:
>
> http://perl-begin.org/uses/multitasking/
>
> Regards,
>
>Shlomi Fish
>
> >
> > Regards
> > ~
> >
> > On 11/3/11, Shlomi Fish  wrote:
> > > On Thu, 3 Nov 2011 06:49:36 +0100
> > > timothy adigun <2teezp...@gmail.com> wrote:
> > >
> > >> Hi a b,
> > >>
> > >> a b  wrote:
> > >>
> > >> > Hi all,
> > >> >
> > >> > i need to track down how much time each function is taking and
> anlyze if
> > >> > threads can help
> > >> >
> > >> > do we have any such function??
> > >> >
> > >>
> > >> **You can use ** use Benchmark qw(:all) **.
> > >> From your CLI you can do: perldoc benchmark,
> > >> or if you not like reading from the command Line Interface, you can
> do:
> > >> perldoc -oHTML -dbenchmark.html benchmark,
> > >> then you have your perldoc benchmark in html format then read how to
> use
> > >> it --
> > >>
> > >
> > > In addition, see:
> > >
> > > http://search.cpan.org/dist/Devel-NYTProf/
> > >
> > > Which is a sophisticated profiler for Perl.
> > >
> > > Other than that, threads may not be the answer due to the way they are
> > > implemented in Perl. Process forking or async IO may be better:
> > >
> > > http://perl-begin.org/uses/multitasking/
> > >
> > > Regards,
> > >
> > > Shlomi Fish
> > >
> > > --
> > > -
> > > Shlomi Fish   http://www.shlomifish.org/
> > > "The Human Hacking Field Guide" - http://shlom.in/hhfg
> > >
> > > Comedy is simply a funny way of being serious.
> > > — http://en.wikiquote.org/wiki/Peter_Ustinov
> > >
> > > Please reply to list if it's a mailing list post -
> http://shlom.in/reply .
> > >
>
>
>
> --
> -
> Shlomi Fish   http://www.shlomifish.org/
> My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/
>
> “You are banished! You are banished! You are banished!
> Hey! I’m just kidding!”
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
>


Re: timings of perl subroutine with a program

2011-11-03 Thread Shlomi Fish
Hello a b,

please reply to the list as I specifically request in my signature. (Wretched
gmail.com.) I'm CCing the list.

On Thu, 3 Nov 2011 16:15:11 +0530
a b  wrote:

> Thanks Shlomi!!
> 
> I am not sure about async I/O?
> 
> any pointers about this one. It is new to me so far
> 

See the links from this URL:

http://perl-begin.org/uses/multitasking/

Regards,

Shlomi Fish

> 
> Regards
> ~
> 
> On 11/3/11, Shlomi Fish  wrote:
> > On Thu, 3 Nov 2011 06:49:36 +0100
> > timothy adigun <2teezp...@gmail.com> wrote:
> >
> >> Hi a b,
> >>
> >> a b  wrote:
> >>
> >> > Hi all,
> >> >
> >> > i need to track down how much time each function is taking and anlyze if
> >> > threads can help
> >> >
> >> > do we have any such function??
> >> >
> >>
> >> **You can use ** use Benchmark qw(:all) **.
> >> From your CLI you can do: perldoc benchmark,
> >> or if you not like reading from the command Line Interface, you can do:
> >> perldoc -oHTML -dbenchmark.html benchmark,
> >> then you have your perldoc benchmark in html format then read how to use
> >> it --
> >>
> >
> > In addition, see:
> >
> > http://search.cpan.org/dist/Devel-NYTProf/
> >
> > Which is a sophisticated profiler for Perl.
> >
> > Other than that, threads may not be the answer due to the way they are
> > implemented in Perl. Process forking or async IO may be better:
> >
> > http://perl-begin.org/uses/multitasking/
> >
> > Regards,
> >
> > Shlomi Fish
> >
> > --
> > -
> > Shlomi Fish   http://www.shlomifish.org/
> > "The Human Hacking Field Guide" - http://shlom.in/hhfg
> >
> > Comedy is simply a funny way of being serious.
> > — http://en.wikiquote.org/wiki/Peter_Ustinov
> >
> > Please reply to list if it's a mailing list post - http://shlom.in/reply .
> >



-- 
-
Shlomi Fish   http://www.shlomifish.org/
My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/

“You are banished! You are banished! You are banished! 
Hey! I’m just kidding!”

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




Re: timings of perl subroutine with a program

2011-11-03 Thread Shlomi Fish
On Thu, 3 Nov 2011 06:49:36 +0100
timothy adigun <2teezp...@gmail.com> wrote:

> Hi a b,
> 
> a b  wrote:
> 
> > Hi all,
> >
> > i need to track down how much time each function is taking and anlyze if
> > threads can help
> >
> > do we have any such function??
> >
> 
> **You can use ** use Benchmark qw(:all) **.
> From your CLI you can do: perldoc benchmark,
> or if you not like reading from the command Line Interface, you can do:
> perldoc -oHTML -dbenchmark.html benchmark,
> then you have your perldoc benchmark in html format then read how to use it --
> 

In addition, see:

http://search.cpan.org/dist/Devel-NYTProf/

Which is a sophisticated profiler for Perl.

Other than that, threads may not be the answer due to the way they are
implemented in Perl. Process forking or async IO may be better:

http://perl-begin.org/uses/multitasking/

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://shlom.in/hhfg

Comedy is simply a funny way of being serious.
— http://en.wikiquote.org/wiki/Peter_Ustinov

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




Re: timings of perl subroutine with a program

2011-11-02 Thread timothy adigun
Hi a b,

a b  wrote:

> Hi all,
>
> i need to track down how much time each function is taking and anlyze if
> threads can help
>
> do we have any such function??
>

**You can use ** use Benchmark qw(:all) **.
>From your CLI you can do: perldoc benchmark,
or if you not like reading from the command Line Interface, you can do:
perldoc -oHTML -dbenchmark.html benchmark,
then you have your perldoc benchmark in html format then read how to use it --


Tim


timings of perl subroutine with a program

2011-11-02 Thread a b
Hi all,

i need to track down how much time each function is taking and anlyze if
threads can help

do we have any such function??


Re: Exit subroutine on filehandle error

2011-07-29 Thread Paul Johnson
On Fri, Jul 29, 2011 at 04:05:26PM +0200, Dr.Ruud wrote:
> On 2011-07-28 00:45, C.DeRykus wrote:
> 
> >   open( ... )  or warn "..." and return;
> 
> Here you are assuming that warn always returns true. It actually
> does, even if the device that it write to is full, but I don't think
> that is documented ...

Heh - I tested that, looked at the docs and then read the source too.
You could still break it, but only with XS I think.  And if you're doing
that sort of thing all bets are off anyway.

However, the whole construct is too ugly and confusing to live.

IMHO, of course.

-- 
Paul Johnson - p...@pjcj.net
http://www.pjcj.net

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




Re: Exit subroutine on filehandle error

2011-07-29 Thread Dr.Ruud

On 2011-07-28 00:45, C.DeRykus wrote:


   open( ... )  or warn "..." and return;


Here you are assuming that warn always returns true. It actually does, 
even if the device that it write to is full, but I don't think that is 
documented ...


--
Ruud

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




Re: Exit subroutine on filehandle error

2011-07-28 Thread Kevin Spencer
On Wed, Jul 27, 2011 at 9:30 AM, Rob Dixon  wrote:
> What exactly is wrong with "or do {...}"?
>
> I believe it is the best option simply because is is comparable to the
> common "open ... or die $!" idiom. The do is there only so that a
> warning can be issued as well as the return

There's nothing wrong with issuing an "or do {...}" especially if you
want to do multiple things on failure.

open(my $fh, '<', $somefile) or do {
log_the_error("Could not open $somefile - $!");
do_something_else();
return;
};

Kevin.

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




Re: Exit subroutine on filehandle error

2011-07-27 Thread C.DeRykus
On Jul 27, 9:30 am, rob.di...@gmx.com (Rob Dixon) wrote:
> ...
> > Well, one thing I dislike about it is that it is using "or do {...}" 
> > instead of
> > an "if ( ) { ... }". And I did mention something similar.
>
> What exactly is wrong with "or do {...}"?
>
> I believe it is the best option simply because is is comparable to the
> common "open ... or die $!" idiom. The do is there only so that a
> warning can be issued as well as the return, and
>

I like do{...} as well but an even simpler alternative in
this case:

  open( ... )  or warn "..." and return;

--
Charles DeRykus


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




Re: Exit subroutine on filehandle error

2011-07-27 Thread Rob Dixon

On 27/07/2011 08:51, Shlomi Fish wrote:

On Tue, 26 Jul 2011 16:58:47 +0100 Rob Dixon  wrote:

On 26/07/2011 16:39, Nikolaus Brandt wrote:

On Tue, Jul 26, 2011 at 01:01:54PM +0300, Shlomi Fish wrote:


Another option would be to use eval { ... } and $@ to trap exceptions:


Thank you all for the replies.

I used the above mentioned eval-$@ solution which was absolutely  working
fine.


I think Shlomi may have been over-thorough in his list of options.


I've mentioned it for completeness sake.


Yes, that is what I assumed. But some of your options are less
appropriate, and I think it would have helped to say so.


Most Perl programmers would shudder at the sight of an eval, and
inthis case it is an ugly implementation of the try/catch idiom.


Eh, why? Have you made a survey that concluded that? I agree that eval { ... }
if ($@) in Perl has its limitations but using such abstractions as
http://search.cpan.org/dist/Exception-Class/ , it is good enough. And I think in
this case, it is appropriate because errors should result in exceptions.


That is a silly comment. You make many assertions yourself Shlomi, but I
wonder how many surveys you have conducted to establish them? It is
unnecessary to turn this into a battle and certainly not what I
intended, so please stop this rivalry.


John's


open my $fh, '>', "$basedir/$userdir/$outfile" or do {
   warn "Can't write: $!\n";
   return;
};


(With or without the warning) will do all that you want, and will
enamour you to all who read your code.



Well, one thing I dislike about it is that it is using "or do {...}" instead of
an "if ( ) { ... }". And I did mention something similar.


What exactly is wrong with "or do {...}"?

I believe it is the best option simply because is is comparable to the
common "open ... or die $!" idiom. The do is there only so that a
warning can be issued as well as the return, and

  open my $fh, '>', "$basedir/$userdir/$outfile" or return undef;

would be fine.

Rob



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




Re: Exit subroutine on filehandle error

2011-07-27 Thread timothy adigun
Hello Nikolaus Brand,
You can try these:
1. ) Instead of "die" in your code use "warn", then return from the
subroutine,
2.) Intstead of hard coding the path and file in your program i.e
["$basedir/$userdir/$outfile" ], ask the user to input the path and file,
assign the input to a scalar and check if it exists/correct or not.
 If the path is correct, then proceed into your subrotine and if not, you
ask the user to check their input and try again! I think that will be a
better way to go!
thanks

On Tue, Jul 26, 2011 at 10:32 AM, Nikolaus Brandt wrote:

> Hi,
>
> I'm currently writing a script which contains a subroutine to write
> data to files.
> Currently I use
> open $fh, '>', "$basedir/$userdir/$outfile" or die "Can't write: $!\n";
> which has the disadvantage, that the whole script dies if e.g. the
> userdir is not available.
>
> Could you give me an advise how to just exit the subroutine if opening
> the filehandle fails, without exiting the whole script?
>
> Thanks in advance.
>
> Nikolaus
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


Re: Exit subroutine on filehandle error

2011-07-27 Thread Shlomi Fish
Hi Rob,

On Tue, 26 Jul 2011 16:58:47 +0100
Rob Dixon  wrote:

> On 26/07/2011 16:39, Nikolaus Brandt wrote:
> > On Tue, Jul 26, 2011 at 01:01:54PM +0300, Shlomi Fish wrote:
> >>
> >> Another option would be to use eval { ... } and $@ to trap exceptions:
> >
> > Thank you all for the replies.
> >
> > I used the above mentioned eval-$@ solution which was absolutely  working
> > fine.
> 
> I think Shlomi may have been over-thorough in his list of options. 

I've mentioned it for completeness sake.

> Most
> Perl programmers would shudder at the sight of an eval, and in this case
> it is an ugly implementation of the try/catch idiom.
> 

Eh, why? Have you made a survey that concluded that? I agree that eval { ... }
if ($@) in Perl has its limitations but using such abstractions as
http://search.cpan.org/dist/Exception-Class/ , it is good enough. And I think in
this case, it is appropriate because errors should result in exceptions.

> John's
> 
> > open my $fh, '>', "$basedir/$userdir/$outfile" or do {
> >   warn "Can't write: $!\n";
> >   return;
> > };
> 
> (With or without the warning) will do all that you want, and will
> enamour you to all who read your code.
> 

Well, one thing I dislike about it is that it is using "or do {...}" instead of
an "if ( ) { ... }". And I did mention something similar.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Funny Anti-Terrorism Story - http://shlom.in/enemy

bzr is slower than Subversion in combination with Sourceforge.
— Sjors, http://dazjorz.com/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




Re: Exit subroutine on filehandle error

2011-07-26 Thread Rob Dixon

On 26/07/2011 16:39, Nikolaus Brandt wrote:

On Tue, Jul 26, 2011 at 01:01:54PM +0300, Shlomi Fish wrote:


Another option would be to use eval { ... } and $@ to trap exceptions:


Thank you all for the replies.

I used the above mentioned eval-$@ solution which was absolutely  working fine.


I think Shlomi may have been over-thorough in his list of options. Most
Perl programmers would shudder at the sight of an eval, and in this case
it is an ugly implementation of the try/catch idiom.

John's


open my $fh, '>', "$basedir/$userdir/$outfile" or do {
  warn "Can't write: $!\n";
  return;
};


(With or without the warning) will do all that you want, and will
enamour you to all who read your code.

Rob




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




Re: Exit subroutine on filehandle error

2011-07-26 Thread Nikolaus Brandt
Hi,

On Tue, Jul 26, 2011 at 01:01:54PM +0300, Shlomi Fish wrote:
> Hi Nikolaus,
> 
> On Tue, 26 Jul 2011 11:32:19 +0200
> Nikolaus Brandt  wrote:
> 
> > Hi,
> > 
> > I'm currently writing a script which contains a subroutine to write
> > data to files.
> > Currently I use
> > open $fh, '>', "$basedir/$userdir/$outfile" or die "Can't write: $!\n";
> > which has the disadvantage, that the whole script dies if e.g. the
> > userdir is not available. 
> > 
> > Could you give me an advise how to just exit the subroutine if opening
> > the filehandle fails, without exiting the whole script?
> > 
> 
> To answer your question, look at the return statement:
> 
> http://perldoc.perl.org/functions/return.html
> 
> You can do:
> 
> if (!open my $fh, '>', $path)
> {
>   return;
> }
> 
> Another option would be to use eval { ... } and $@ to trap exceptions:
> 
> http://perl-begin.org/tutorials/perl-for-newbies/part4/#page--exceptions--DIR
> 
> Now a few comments on your code:
> 
> 1. Normally, you should do: "open my $fh" instead of "open $fh" to limit its
> scope.
Done.
> 
> 2. You're interpolating several variables into a
> path: "$basedir/$userdir/$outfile", so make sure you sanitise them. If I put 
> in
> $outfile e.g: "../../../../etc/passwd", then I'll be able to write
> to /etc/passwd. See:
I added a check to verify there's no naughty stuff going on.
> 
> http://webcache.googleusercontent.com/search?q=cache:aEbtJ4YXhVkJ:shlomif-tech.livejournal.com/35301.html%3Fthread%3D29157+code+markup+injection+prevention&cd=1&hl=en&ct=clnk&source=www.google.com
> 
> (sorry - livejournal.com is down.)
> 
> You may also opt to use what Joel Spolsky describes here:
> 
> http://www.joelonsoftware.com/articles/Wrong.html
> 
> or perhaps a superior method of making the wrong code behave in an obviously
> wrong way (i.e: terminate the program with an error), which will require more
> coding in Perl.
> 
> Regards,
> 
>   Shlomi Fish
> 
> -- 
> -
> Shlomi Fish   http://www.shlomifish.org/
> My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/
> 
> Tcl is Lisp on drugs. Using strings instead of S‐expressions for closures is
> Evil with one of those gigantic E’s you can find at the beginning of chapters.
> 
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
> 

Thank you all for the replies.
I used the above mentioned eval-$@ solution which was absolutely
working fine.

Thanks again!

Nikolaus

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




Re: Exit subroutine on filehandle error

2011-07-26 Thread John W. Krahn

Nikolaus Brandt wrote:

Hi,


Hello,


I'm currently writing a script which contains a subroutine to write
data to files.
Currently I use
open $fh, '>', "$basedir/$userdir/$outfile" or die "Can't write: $!\n";
which has the disadvantage, that the whole script dies if e.g. the
userdir is not available.

Could you give me an advise how to just exit the subroutine if opening
the filehandle fails, without exiting the whole script?


Yes, just exit (return) from the subroutine:

open my $fh, '>', "$basedir/$userdir/$outfile" or do {
warn "Can't write: $!\n";
return;
};



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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Exit subroutine on filehandle error

2011-07-26 Thread Miquel Ruiz

El 26/07/2011 12:01, Shlomi Fish escribió:

Another option would be to use eval { ... } and $@ to trap exceptions:

http://perl-begin.org/tutorials/perl-for-newbies/part4/#page--exceptions--DIR



Important to remember that "open" won't raise an exception if you are 
not using the "autodie" pragma, which you can enable with:


use autodie;

(http://perldoc.perl.org/autodie.html)

Best regards,

--
Miquel Ruiz

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




Re: Exit subroutine on filehandle error

2011-07-26 Thread Shlomi Fish
Hi Nikolaus,

On Tue, 26 Jul 2011 11:32:19 +0200
Nikolaus Brandt  wrote:

> Hi,
> 
> I'm currently writing a script which contains a subroutine to write
> data to files.
> Currently I use
> open $fh, '>', "$basedir/$userdir/$outfile" or die "Can't write: $!\n";
> which has the disadvantage, that the whole script dies if e.g. the
> userdir is not available. 
> 
> Could you give me an advise how to just exit the subroutine if opening
> the filehandle fails, without exiting the whole script?
> 

To answer your question, look at the return statement:

http://perldoc.perl.org/functions/return.html

You can do:

if (!open my $fh, '>', $path)
{
return;
}

Another option would be to use eval { ... } and $@ to trap exceptions:

http://perl-begin.org/tutorials/perl-for-newbies/part4/#page--exceptions--DIR

Now a few comments on your code:

1. Normally, you should do: "open my $fh" instead of "open $fh" to limit its
scope.

2. You're interpolating several variables into a
path: "$basedir/$userdir/$outfile", so make sure you sanitise them. If I put in
$outfile e.g: "../../../../etc/passwd", then I'll be able to write
to /etc/passwd. See:

http://webcache.googleusercontent.com/search?q=cache:aEbtJ4YXhVkJ:shlomif-tech.livejournal.com/35301.html%3Fthread%3D29157+code+markup+injection+prevention&cd=1&hl=en&ct=clnk&source=www.google.com

(sorry - livejournal.com is down.)

You may also opt to use what Joel Spolsky describes here:

http://www.joelonsoftware.com/articles/Wrong.html

or perhaps a superior method of making the wrong code behave in an obviously
wrong way (i.e: terminate the program with an error), which will require more
coding in Perl.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/

Tcl is Lisp on drugs. Using strings instead of S‐expressions for closures is
Evil with one of those gigantic E’s you can find at the beginning of chapters.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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




Exit subroutine on filehandle error

2011-07-26 Thread Nikolaus Brandt
Hi,

I'm currently writing a script which contains a subroutine to write
data to files.
Currently I use
open $fh, '>', "$basedir/$userdir/$outfile" or die "Can't write: $!\n";
which has the disadvantage, that the whole script dies if e.g. the
userdir is not available. 

Could you give me an advise how to just exit the subroutine if opening
the filehandle fails, without exiting the whole script?

Thanks in advance.

Nikolaus

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




Re: how do I pass arrays from mainscript into the subroutine

2011-05-09 Thread Jim Gibson
On 5/9/11 Mon  May 9, 2011  8:03 AM, "Sandip Bhattacharya"
 scribbled:

>> 
>> Also, don't call a sub with the &.  Does can do unexpected things.
>> 
> 
> Just for my understanding, what are the other issues with &sub apart
> from skipping prototype definitions?

Please read the descriptions in 'perldoc perlsub'. Search for "Subroutines
may be called recursively' and the section titled 'Prototypes'.
Specifically, the use of the &sub form means that prototype checking will
not be done and, if no arguments are included in the subroutine call, the @_
array will not be initialized and will be whatever value it had at the time
of the call.

While the use of subroutine prototypes is rare and the absence of checking
not often a problem, having an unexpected value in @_ could cause unintended
results.



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




  1   2   3   4   5   6   7   8   9   10   >