Re: Comment blocks & private methods
by LanX (Saint) on Dec 12, 2014 at 15:32 UTC
|
For multiline comments use pod² or void here-docs¹.
Beside leading underscore convention real private methods are introduced in newer Perl5 versions via my , all older allow putting a function ref into a private var.
my $private = sub { print "privat" };
$private->();
All this has been extensively discussed in the past, did you try searching the archives?
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
updates
¹)
<<___;
bla
bla
bla
___
²)
print "huhu";
=for doc
bla
bla
bla
=cut
print "haha";
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Comment blocks & private methods
by toolic (Bishop) on Dec 12, 2014 at 15:34 UTC
|
| [reply] [Watch: Dir/Any] |
|
A note about this: One of the suggestions is to use the -P option to tell Perl to use the C preprocessor, thus enabling use of C style comments. This option seems to have been removed at some point. At least in 5.18.2, I get the error "Unrecognized switch: -P".
| [reply] [Watch: Dir/Any] [d/l] |
Re: Comment blocks & private methods
by AnomalousMonk (Archbishop) on Dec 12, 2014 at 16:58 UTC
|
The 'classic' Perl private method hack (IIRC) is an anonymous subroutine reference (update: stored in a lexical my variable) in a module (or indeed, within the limitation of any scope).
c:\@Work\Perl>perl -wMstrict -le
"my $ps = sub { my $class = shift; print qq{obj/class: $class (@_)} }
+;
;;
Foo->$ps;
'Quux'->$ps(1, 2, 3, 'foo', 'bar');
;;
my $o = bless [];
$o->$ps(9, 8, 7);
"
obj/class: Foo ()
obj/class: Quux (1 2 3 foo bar)
obj/class: main=ARRAY(0x35c06c) (9 8 7)
See The Arrow Operator in perlop.
Update 1: Of course, that's just your basic, OOTB Perl. If you avail yourself of one of the many, fine OO support/extension modules/bundles, you may find explicit private method support. Can't think of an example offhand...
Update 2: Reading over this thread again, I notice now that LanX has already covered this hack above. Oh, well...
| [reply] [Watch: Dir/Any] [d/l] |
Re: Comment blocks & private methods
by Eily (Monsignor) on Dec 12, 2014 at 16:03 UTC
|
There are several solutions for multiline comments like turning a section of code into a string with q or HEREDOC, POD, or source filters if (0). The first category will trigger a warning in most cases (You could always start all your comments with 'igor' or 'dilbert' though :P). The second puts unused code in what should be useful documentation. The third uses source filters, which are impopular among several perl users (mostly because can cause confusing errors, since the code compiled by perl is not the one in the text file). The if (0) solution doesn't work on everything (if there's a BEGIN block somewhere in the commented code, it will still be executed), and won't compile if there's a syntax error in the disabled code.
To me the best solution still is source filters, but since most editors allow you to easily comment several lines of code at once, I just go for that (ctrl+Q on selected code on my version of notepad++, or other shortcuts depending on exactly what you want to do).
Edit: and for private methods, well LanX actually gave you the answer.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
> To me the best solution still is source filters
Actually I agree here, cause I can't possibly see a problem in this case, since comments ARE sourcefilters! :)
Sourcefilters arent bad by definition, it depends on the use case, and this one is legit!
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
update
I have to revoke my opinion, source filters can't distiguish between multi line strings and code.
But pod doesn't have this problem:
$string = "
bla
=for doc
a source filter would delete this part from the string
=cut
bla
";
| [reply] [Watch: Dir/Any] [d/l] |
|
Except to parse comments correctly you have to parse perl, because knowing where a string (or string-like anything) starts and ends is really complicated (there are so many ways to start a string in perl that can be nested into each other that you have to parse the whole file correctly to be sure wether one part is code or a string), you wouldn't want your parser to change:
"You can also use C-like comments: /* */" into "You can also use C-like comments: " or /\/*\*// # yup, good coding style into /\/ # that works just fine.
I didn't have that point in mind when I wrote my comment though.
Edit: in the end yes, POD may actually be the best idea. My bad :D
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
|
|
|
Re: Comment blocks & private methods
by Anonymous Monk on Dec 12, 2014 at 16:09 UTC
|
#1 is a complaint about your workflow, "I have to comment". What are you using to edit Perl files? My guess is whatever editor you're using (other than notepad and the like) there is a swift way to comment a block of lines quickly and efficiently. | [reply] [Watch: Dir/Any] |
Re: Comment blocks & private methods
by misterperl (Pilgrim) on Dec 12, 2014 at 15:59 UTC
|
Yes I did see some info in various forums, but not so much for
[private]..
I'm not sure what you mean by *search the archives*, unless that is a psudeonym for *super-search* which is all I see as a selection.
SOME DAY I hope I JUST get an answer here, instead of "you're wrong to even ask this question, but....". Now the LINK - THAT was helpful thanks!
Cheers.. | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
> SOME DAY I hope I JUST get an answer here, instead of "you're wrong to even ask this question, but...."
yeah maybe you are right and I should simply stop answering questions which expect ME to do the searching ...
super search points you to http://www.google.com/advanced_search?hl=en&as_sitesearch=www.perlmonks.org
please explain whats wrong with the queries "multi-line comment" and "private methods".
The better the question the better the answer.
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
| [reply] [Watch: Dir/Any] |
|
SOME DAY I hope I JUST get an answer here, instead of "you're wrong to even ask this question, but....". Now the LINK - THAT was helpful thanks!
I used to feel that way too, then later on (hindsight's 20-20 right?) I realized that the lack of hand-holding here made me *much* stronger in perl than I was before. Rather than having the answer spoon-fed to me, I had to figure it out (and even moreso here than other forum sites I think) and in the end it makes a much better coder out of you.
Either way, I hope you found a viable solution.
| [reply] [Watch: Dir/Any] |
|
There is no problem asking old questions again, but some are interested to know why the old answers failed.
Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
| [reply] [Watch: Dir/Any] |