Perl using a variable to reference a module messes up passing parameters -


i have problem when using variable reference module, seems mess passing of variables:

toto.pm

package toto;  use data::dumper;  sub print {        print dumper(@_); } 

perl program

package main;  toto::print('hello world'); print ">>>>>>>>>>>\n"; $package = 'toto';  $package->print('hello world'); 

and output is:

$var1 = 'hello world'; >>>>>>>>>>> $var1 = 'toto'; $var2 = 'hello world'; 

any advice on how avoid having toto passed first variable?

short: observed behavior comes use of -> on package name.

the arrow operator used reference or object, reference data structure has been bless-ed class. (or class name, see below.) object or class name quietly passed first argument whole system work. note package in question not define class (objects cannot created it).

from arrow operator in perlop

"-> " infix dereference operator, in c , c++. if right side either [...] , {...} , or (...) subscript, left side must either hard or symbolic reference array, hash, or subroutine respectively. (or technically speaking, location capable of holding hard reference, if it's array or hash reference being used assignment.) see perlreftut , perlref.

it continues, statements of direct interest in problem

otherwise, right side method name or simple scalar variable containing either method name or subroutine reference, , left side must either object (a blessed reference) or class name (that is, package name). see perlobj.

so in uses related classes left-hand side may contain class name, , class methods can invoked on (or can queried). given class package is package name.

the situation in question falls within package name passed subroutine. however, according above quote seems sub can method, isn't case here. may use of -> should disallowed. either way, using on package isn't class strikes me mistaken.


update clarification. use intended, resolve ambiguity in package loaded. package name saved variable , sub invoked on it, using arrow operator. in case code have added sub handle first argument (package name) passed regardless of invocation, courtesy of arrow operator. then have allow case when is invoked on object, ending code covers 2 distinct uses. believe better change design not involve this.


if want use package, library

file toto.pm

pacakge toto;  use exporter; our (@isa, @export_ok); @isa = ('exporter'); @export_ok = qw(prn);   # can asked user of package  use data::dumper;  sub prn {        print dumper(@_); }  1;  # important 'require' when used 

i've changed sub name prn it's not perl library function. main script

use warnings; use strict;  use toto qw(prn);  prn("hello world"); 

the qualified name toto::prn() can used. if wanted make class require bit more in package.

this package, toto, not export default, unless asked for. that's @export_ok sets , that's why need list functions import main:: when use toto. start, example, perlmod


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -