in reply to Formats and Variable Scope

Hi,
IIRC, formats are 'global': hyding them in a sub (more generaly in a block) does not do any good, just like subs' declarations, they are visible through the program.

As sugested, you could use formline, but this has the inconvenience that you have to do your own page breaking.

What I usually do, is defining the format using $_[0], $_[1],..., $_[n] as variables (as @_ is globaly defined, they are always in scope), then define a sub wich just calls write (the sub is there to put the data into @_).

format admin_usage_data = @<<<<<<< @<<<<<<< @<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<< $_[0], $_[1], $_[2], $_[3] sub write_admin_usage_data { write(); } write_admin_usage_data($username,$login,$logout,$activity);

Then you could put all the formats relating to a given report into a module definition together with a sub activating the formats (which will take a filehandle) and a writeline sub to write.

You could even make it object oriented and a subclass of IO::File so that using of formats is disjoint from the logic of data acquisition.

I apologize if this is confused, but here I have no Perl, so I can not try something and post the code...

Hoping this is usefull

Cheers


Leo TheHobbit

update (broquaint): added <code> tags to escape square brackets

Replies are listed 'Best First'.
Re: Re: Formats and Variable Scope
by chromatic (Archbishop) on Nov 19, 2002 at 21:11 UTC

    That's pretty clever, I like it! Go one step further and define named constants, and it's even readable:

    use constant USERNAME => 0; use constant LOGIN => 1; use constant LOGOUT => 2; use constant ACTIVITY => 3; format admin_usage_data = @<<<<<<< @<<<<<<< @<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<< $_[USERNAME], $_[LOGIN], $_[LOGOUT], $_[ACTIVITY]