Hi,
> How do I get this to work?
well, let's try...
> package main;
> use Simple.pm;
if you define the package in the same file, you dont do 'use Simple': perl
will then look in @INC for a 'Simple.pm' file,
which you dont have.
ALSO the syntax is 'use ModuleName' - so no '.pm' at the end!
> my $var = "hello!";
> slog("This is my message: $var.");
you'll only have 'slog' if you actually put the module in a seperate file
and use it... otherwise you have to use a fully qualified name,
say: Simple::slog( 'vars' );
> package Simple;
> use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
> require Exporter;
'use' would be appropriate here - it will be checked at compile time.
'use' is equivalent to: BEGIN { require Module; import Module LIST; } ###
perldoc -f use
> @ISA = qw(Exporter);
> @EXPORT = qw( slog );
> sub slog {
>
> my $msg = @_;
this will NOT do what you want
if you say $foo = @bar, $foo will hold the LENGTH of the array... in your
case, $msg will be '1', seeing you passed 1 string...
> $msg_format =~ s/(\$\w+/$1/eeg;
i have NO clue what you want to do here...
but there's a syntax error in there: you never finish the ( ) capturing!
> print $msg_format;
> }
>
> Instead of module Simple seeing the main variable, it's trying to display
> one local to it's own package. Ultimately, what I'd like to do is:
ehm, no it's not... $var gets interpolated when you call the
slog("This is my message: $var.");
slog will only see: "This is my message: hello!." as the first (and only)
element of @_
> - pass a localized variable to a module in a quoted string
You're doing that... well, sorta... 'package Simple' is not a module, but i
explained that above.
if you want some more insight in how modules work, and how you should write
them, perhaps my tutorial is a good place to start:
http://japh.nu/index.cgi?base=tuts has a few beginners tutorials, including
ones for OO programming.
> - the value of the quoted variable is then inserted in a message.
that's no problem, see my example code below
> I've played around with (caller())[0], but couldn't find the magic combo.
what magic combo were you looking for?
### working code ###
#!/usr/bin/perl -w #ALWAYS use warnings!
use strict; # ALWAYS use strict!
package main;
my $var = "hello!";
Simple::slog("This is my message: $var.");
package Simple;
sub slog {
my ($msg) = @_; # dont forget the ( ) around $msg to force list context!
print $msg;
#$msg_format =~ s/(\$\w+)/$1/eeg; # this regexp is not matching!
#print $msg_format;
print "\nI just got:\n --- $msg ---\n"
}
hth,
Jos
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]