Re: Use vs Require with version number

2015-03-05 Thread SSC_perl
On Mar 4, 2015, at 8:15 PM, Brandon McCaig wrote:
 
 That could matter in rare, silly cases. In most cases, it
 wouldn't really matter (usually we require modules and assert
 versions at the beginning of a program or module before anything
 else is actually done).

That explains it.  Thanks.  I wasn't aware that the version requirement 
might be added later in the script.

Frank

SurfShopCART 2.0  Coming Soon...
http://www.surfshopcart.com/store/demo/surfshop/shop.cgi


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




Re: Use vs Require with version number

2015-03-05 Thread Kent Fredric
On 5 March 2015 at 17:15, Brandon McCaig bamcc...@gmail.com wrote:

 Uri means that use is
 effectively requiring the module with a BEGIN block. That means
 that it will execute before any other code that isn't in a BEGIN
 block.



It may also be worth mentioning that BEGIN is actually a sub. A special
sub that doesn't need the sub keyword.

Calling them blocks here will allow you to overlook certain problems,
like potential closure behaviour closing over variables.

This is usually not a problem in practice because perl typically parses the
BEGIN sub, executes it, and then discards it, but there's an internal flag
to perl that can disable the discarding, and the results are incredibly
surprising if you don't remember this fact:
http://www.nntp.perl.org/group/perl.perl5.porters/2015/02/msg226031.html


-- 
Kent

*KENTNL* - https://metacpan.org/author/KENTNL


Use vs Require with version number

2015-03-04 Thread SSC_perl
Hi all,

I'm just curious about something.  What's the difference between using

require 5.016;

or

use 5.016;

The only thing I've seen is that if 5.16 isn't installed, 'use' outputs:

Perl v5.16 required--this is only v5.10.1, stopped at shop.cgi line 26.
BEGIN failed--compilation aborted at shop.cgi line 26.

while 'require' outputs this:

Perl v5.16 required--this is only v5.10.1, stopped at shop.cgi line 26.

I know there's a difference when calling a module, but what about the 
version number?  Is it just semantics or is there something more to it?  My 
search hasn't revealed an answer. 

Thanks,
Frank

SurfShopCART 2.0  Coming Soon...
https://www.surfshopcart.com/store/demo/surfshop/shop.cgi


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




Re: Use vs Require with version number

2015-03-04 Thread Uri Guttman

On 03/04/2015 09:12 PM, SSC_perl wrote:

Hi all,

I'm just curious about something.  What's the difference between using

require 5.016;

or

use 5.016;

The only thing I've seen is that if 5.16 isn't installed, 'use' outputs:

Perl v5.16 required--this is only v5.10.1, stopped at shop.cgi line 26.
BEGIN failed--compilation aborted at shop.cgi line 26.

while 'require' outputs this:

Perl v5.16 required--this is only v5.10.1, stopped at shop.cgi line 26.

I know there's a difference when calling a module, but what about the 
version number?  Is it just semantics or is there something more to it?  My 
search hasn't revealed an answer.



it is more about when the check is done. use is done at compile time and 
require is done at run time. also use effectively calls require to load 
the module and then it may do importing as well. when a module is loaded 
it will run any use lines during its compile and require lines once the 
module is run. but from the point of view of the code that loaded that 
module both will cause failure if the version is not supported.


uri






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




Re: Use vs Require with version number

2015-03-04 Thread SSC_perl
On Mar 4, 2015, at 6:14 PM, Uri Guttman wrote:
 
 it is more about when the check is done. use is done at compile time and 
 require is done at run time. also use effectively calls require to load the 
 module and then it may do importing as well. when a module is loaded it will 
 run any use lines during its compile and require lines once the module is 
 run. but from the point of view of the code that loaded that module both will 
 cause failure if the version is not supported.

So there's only really a difference for loading modules, not for 
setting the minimum version number?

Thanks,
Frank

SurfShopCART 2.0  Coming Soon...
http://www.surfshopcart.com/store/demo/surfshop/shop.cgi


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




Re: Use vs Require with version number

2015-03-04 Thread Uri Guttman

On 03/04/2015 11:15 PM, Brandon McCaig wrote:
I think that generally you should be using `use' unless you have a 
specific need to use require directly. `use' will call require() under 
the surface when needed so to you it's basically the same, but it has 
added benefits that make sense generally. If you want the action to 
happen at run-time then require() may be more appropriate, but those 
cases should be rare. Similarly, require is sort of like a wrapper 
over `do FILENAME' so there may also be cases where you'd want to use 
`do' instead, but most of the time use is more appropriate, and less 
often require is more appropriate. For the detailed explanation take 
the time to read through the following: perldoc perlmod (search for 
BEGIN) perldoc -f use perldoc -f require perldoc -f do Then use `use' 
until you know you need something else. :) Regards, 


what you said is good. i just want to emphasize about compile vs run 
time in perl. perl being a dynamic language doesn't have the classic 
compile to machine code and then run phases of langs like c and c++. a 
BEGIN block runs immediately after it is compiled, before any regular 
code in the file. as said, use is like require in a BEGIN block (plus 
the importing). and the other direction is eval STRING which compiles 
and runs code at run time. and if you call eval STRING in a BEGIN block 
you are compiling code (and running it) at run time of the code of the 
BEGIN block. it can be tricky to follow that but it all makes sense. any 
block of perl has to be compiled. and then it gets run. BEGIN and eval 
STRING allow you to control when those phases happen. and there are 
powerful uses for that but rarely do beginners need them. eval STRING is 
so dangerous that i always say you shouldn't use it until you know when 
not to use it. same for symrefs.


uri


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




Re: Use vs Require with version number

2015-03-04 Thread Brandon McCaig
On Wed, Mar 04, 2015 at 06:26:41PM -0800, SSC_perl wrote:
 So there's only really a difference for loading modules, not
 for setting the minimum version number?

There could be a difference if code with side effects is done
first. By being done at compile-time, Uri means that use is
effectively requiring the module with a BEGIN block. That means
that it will execute before any other code that isn't in a BEGIN
block. That could matter in rare, silly cases. In most cases, it
wouldn't really matter (usually we require modules and assert
versions at the beginning of a program or module before anything
else is actually done).

A silly, contrived program might be:

use strict;
use warnings;

use My::Stuff;

my $stuff = My::Stuff-new();

$stuff-do_consequences();

require v5.018;

$stuff-more_consequences();

__END__

If your entire program requires 5.018 to work properly, but
perhaps My::Stuff::do_consequences will do something *bad* if run
under a previous version of perl then this program would be
*dangerous*. The require() is going to happen at run-time *after*
the do_consequences method is called. If I had used `use' instead
then it would have happened at compile-time, within a BEGIN
block, and do_consequences never would have been called.

Here is that program again using `use' for the version assertion:

use strict;
use warnings;

use My::Stuff;

my $stuff = My::Stuff-new();

$stuff-do_consequences();

use v5.018; ### ***

$stuff-more_consequences();

__END__

This is sort of like:

BEGIN { require strict; }
BEGIN { require warnings; }

BEGIN {
require My::Stuff;
My::Stuff-import();
}

my $stuff = My::Stuff-new();

$stuff-do_consequences();

BEGIN {
require v5.018;
}

$stuff-more_consequences();

__END__

BEGIN blocks are always executed before surrounding code so you
can think of this program as:

require strict;
require warnings;

require My::Stuff;
My::Stuff-import();

require v5.018; ### ***

my $stuff = My::Stuff-new();

$stuff-do_consequences();

$stuff-more_consequences();

__END__

This is an over-simplification. Hell, I probably don't fully
understand the mechanism either, but the idea is that the BEGIN
stuff happens first. Even though within my code I had the
do_consequences method call before the v5.018 assertion the
actual result is that the Perl version is checked first, and if
dissatisfied the program will die() right then and there before
any consequences can be carried out.

I think that generally you should be using `use' unless you have
a specific need to use require directly. `use' will call
require() under the surface when needed so to you it's basically
the same, but it has added benefits that make sense generally. If
you want the action to happen at run-time then require() may be
more appropriate, but those cases should be rare.

Similarly, require is sort of like a wrapper over `do FILENAME'
so there may also be cases where you'd want to use `do' instead,
but most of the time use is more appropriate, and less often
require is more appropriate.

For the detailed explanation take the time to read through the
following:

perldoc perlmod (search for BEGIN)
perldoc -f use
perldoc -f require
perldoc -f do

Then use `use' until you know you need something else. :)

Regards,


-- 
Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org
Castopulence Software https://www.castopulence.org/
Blog http://www.bambams.ca/
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'



signature.asc
Description: Digital signature