Hi,

Thank you for your answer. I'm trying to use subs for a better looking HTML 
and XML manipulation. I know there are several ways to pass parameters, but 
as I said, I'm just trying to get a better looking code. For example, using 
prototypes, I'm able to build an HTML table this way:

--------------------------
Table {
    Tr {
        Td {
            # ...code or constant context...
        };
        Td {
            # ...code or constant context...
        };
    };
    Tr {
        Td {
            # ...code or constant context...
        };
        Td {
            # ...code or constant context...
        };
    };
};
---------------------

That I already have done and working perfectly, and all HTML output is 
generated correctly. However, I'd like to be able to do that specifying 
optional properties for each tag. For example:

--------------------------
Table (border => 0, cellpadding => 4) {
    Tr {
        Td (width => 31) {
            # ...code or constant context...
        };
        Td (valign => 'top') {
            # ...code or constant context...
        };
    };
    Tr {
        Td {
            # ...code or constant context...
        };
        Td (class => 'lastCell') {
            # ...code or constant context...
        };
    };
};
---------------------

That would keep the great readable looking and would allow me to specify 
important tag properties, specially for XML output.

I know there are plenty of modules that use somehow similar ways to 
construct HTML or XML, even CGI.pm, but none of them has this clear syntax - 
you have to pass all parameters like in your example.

Well, I was just exploring posibilities with prototypes, and since I got the 
first example working great, I just wanted to know if there was a way to 
acomplish the second example. According to perlsub, prototypes allow you to 
create your own syntax to "copy" the way that builtin functions work. So, 
this would be a "copy" of a  "for ( ... ) { ... }" syntax, except that it 
would not do a loop but other things.

Actually, using prototypes, I found the way to pass values after the first 
block of code (e.g. table { ...code... } properties { ...code... };), kinda 
the eval-catch example in perlsub, but that doesn't really keep the concept 
of readability that I tried to get in the second example.

Thank you anyway for your answer. :-)


Cheers,

Paco Zarabozo






------------------------------------------------------------------------------------------
From: Foo JH
Sent: Saturday, March 22, 2008 11:25 AM
To: Zarabozo, Francisco (GE, Corporate)
Cc: Active State Perl Mailing List
Subject: Re: Perl Prototypes


If you're trying to write a sub with default params, you can try this:

sub mysub
{
my ($a,$b,$c) = @_;

$a ||= 1;
$b ||= 2;
$c ||= 3;
}

Zarabozo, Francisco (GE, Corporate) wrote:
> Hello All,
>
> After reading a lot on the subject, I'm guessing that it's not possible to
> do what I want to do, but anyway I wanted to ask you about and hopefuly be
> wrong. :-)
>
> I want to write a sub so I can use it like this:
>
> --------------------------------
> mysub (a => 1, b => 2, c => 3) {
>    # ...CODE...
> };
> --------------------------------
>
> But, I haven't found any valid prototype combination to make that work.
> After reading the perlsub documentation, at first I thought I would be 
> able
> to do so like this:
>
> --------------------------------
> sub mysyb (\%&) {
>    # ...CODE...
> }
> --------------------------------
>
> But, of course, I was wrong. Taking out the backslash is also wrong since
> the % will eat anything after it.
>
>
> Please let me know if any of you know the way to correctly construct such
> prototype.
>
> Thanks in advance.
>
>
> Cheers, :-)
>
> Paco
>
>
>
> _______________________________________________
> ActivePerl mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to